Skip to content Skip to sidebar Skip to footer

How Can I Transform A Bitmap Into A Uri?

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:

Solution 1:

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) {
  ByteArrayOutputStreambytes=newByteArrayOutputStream();
  inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  Stringpath= MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}

Solution 2:

StringFILENAME="image.png";
StringPATH="/mnt/sdcard/"+ FILENAME;
Filef=newFile(PATH);
UriyourUri= Uri.fromFile(f);

Solution 3:

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=newFile(tempDir.getAbsolutePath()+"/.temp/");
    tempDir.mkdir();
    FiletempFile= File.createTempFile(title, ".jpg", tempDir);
    ByteArrayOutputStreambytes=newByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    byte[] bitmapData = bytes.toByteArray();

    //write the bytes in fileFileOutputStreamfos=newFileOutputStream(tempFile);
    fos.write(bitmapData);
    fos.flush();
    fos.close();
    return Uri.fromFile(tempFile);

Solution 4:

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

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

    Stringpath= MediaStore.Images.Media.insertImage(getContentResolver(), src, "title", null);
    return Uri.parse(path);
}

Solution 5:

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.bmpRelative: yourImage.bmp 

Post a Comment for "How Can I Transform A Bitmap Into A Uri?"