40

I'm trying to share images with Facebook, twitter, etc using SHARE INTENT from Android.

I found code to send a image to the share intent, but this code needs the URI of the bitmap: fullSizeImageUri

This is the full code:

private void startShareMediaActivity(Bitmap image) {
    boolean isVideo=false;
    String mimeType="bmp";
    Uri fullSizeImageUri=null;
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType(mimeType);
    intent.putExtra(Intent.EXTRA_STREAM, fullSizeImageUri);
    try {
        startActivity(Intent.createChooser(intent, (isVideo ? "video" : "image")));
    } catch (android.content.ActivityNotFoundException ex) { }
}

How to transform a Bitmap into a Uri?

peterh
  • 1
  • 15
  • 76
  • 99
NullPointerException
  • 34,337
  • 71
  • 211
  • 357

6 Answers6

84

Here is the Colin's Blog who suggest the simple method to convert bitmap to Uri Click here

public Uri getImageUri(Context inContext, Bitmap inImage) {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}
DragonFire
  • 2,836
  • 1
  • 23
  • 40
Ajay
  • 4,730
  • 2
  • 30
  • 43
22
String FILENAME = "image.png";
String PATH = "/mnt/sdcard/"+ FILENAME;
File f = new File(PATH);
Uri yourUri = Uri.fromFile(f);
KK_07k11A0585
  • 2,389
  • 3
  • 24
  • 33
4

The above solution uses media store and stores the image in the users main image folder making it viewable through the gallery/photo viewer. This solution will store it as a temporary file in your apps data. In this example inImage is a Bitmap and title is a string for the name of the image file.

    File tempDir= Environment.getExternalStorageDirectory();
    tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
    tempDir.mkdir();
    File tempFile = File.createTempFile(title, ".jpg", tempDir);
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    byte[] bitmapData = bytes.toByteArray();

    //write the bytes in file
    FileOutputStream fos = new FileOutputStream(tempFile);
    fos.write(bitmapData);
    fos.flush();
    fos.close();
    return Uri.fromFile(tempFile);
ocross
  • 582
  • 1
  • 5
  • 23
  • How temporary is temporary? When will the file be deleted? Can I use the same file name for several images in quick succession? – Joel Broström Mar 24 '20 at 19:25
  • instead of tempDir=new File(tempDir.getAbsolutePath()+"/.temp/"); you can always use a directory exclusively used by your app.. and just leave the images there – DragonFire Jun 23 '20 at 05:02
  • @Joel Also if you are using in a loop name the file name as "file"+i and on success of your process like an api response you can clear this folder -> that will have to be done manually – DragonFire Jun 23 '20 at 05:19
1

pass bitmap and compressFormat like (PNG, JPG, etc...) and image quality in percentage

public Uri getImageUri(Bitmap src, Bitmap.CompressFormat format, int quality) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    src.compress(format, quality, os);

    String path = MediaStore.Images.Media.insertImage(getContentResolver(), src, "title", null);
    return Uri.parse(path);
}
Pankaj Talaviya
  • 3,030
  • 26
  • 30
0
String picName = "pic.jpg";
        String PATH = Environment.getExternalStorageDirectory().getPath()+ picName;
        File f = new File(PATH);
        Uri yourUri = Uri.fromFile(f);
mehrdad khosravi
  • 2,222
  • 9
  • 29
  • 34
0

Well you can't transforma a bitmap file into a uri. Read more about URI here

URI is an Uniform Resource Identifier. But you can place the bitmap in an absolute or relative URI like this

Absolute: http://android.com/yourImage.bmp
Relative: yourImage.bmp 
Aurelian Cotuna
  • 3,018
  • 3
  • 27
  • 48