NullPointerException
NullPointerException

Reputation: 37577

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: 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?

Upvotes: 48

Views: 120094

Answers (8)

Uzzam Altaf
Uzzam Altaf

Reputation: 393

The latest solution I found is this. Its in kotlin you can covert it in Java.

    // Get uri of images from camera function
private fun getImageUri(inContext: Context?, inImage: Bitmap): Uri {

    val tempFile = File.createTempFile("temprentpk", ".png")
    val bytes = ByteArrayOutputStream()
    inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes)
    val bitmapData = bytes.toByteArray()

    val fileOutPut = FileOutputStream(tempFile)
    fileOutPut.write(bitmapData)
    fileOutPut.flush()
    fileOutPut.close()
    return Uri.fromFile(tempFile)
}

Upvotes: 6

val context = LocalContext.current
val uri = context.saveImage(bitmap) // here you will receive the Uri


private fun Context.saveImage(bitmap: Bitmap): Uri? {
    var uri: Uri? = null
    try {
        val fileName = System.nanoTime().toString() + ".png"
        val values = ContentValues().apply {
            put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
            put(MediaStore.Images.Media.MIME_TYPE, "image/png")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/")
                put(MediaStore.MediaColumns.IS_PENDING, 1)
            } else {
                val directory =
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                val file = File(directory, fileName)
                put(MediaStore.MediaColumns.DATA, file.absolutePath)
            }
        }

        uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
        uri?.let {
            contentResolver.openOutputStream(it).use { output ->
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, output)
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                values.apply {
                    clear()
                    put(MediaStore.Audio.Media.IS_PENDING, 0)
                }
                contentResolver.update(uri, values, null, null)
            }
        }
        return uri
    }catch (e: java.lang.Exception) {
        if (uri != null) {
            // Don't leave an orphan entry in the MediaStore
            contentResolver.delete(uri, null, null)
        }
        throw e
    }
}

Upvotes: 3

Ajay
Ajay

Reputation: 4926

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);
}

Upvotes: 96

Pankaj Talaviya
Pankaj Talaviya

Reputation: 3458

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);
}

Upvotes: 1

mehrdad khosravi
mehrdad khosravi

Reputation: 2258

String picName = "pic.jpg";
        String PATH = Environment.getExternalStorageDirectory().getPath()+ picName;
        File f = new File(PATH);
        Uri yourUri = Uri.fromFile(f);

Upvotes: 0

ocross
ocross

Reputation: 613

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);

Upvotes: 4

KK_07k11A0585
KK_07k11A0585

Reputation: 2403

String FILENAME = "image.png";
String PATH = "/mnt/sdcard/"+ FILENAME;
File f = new File(PATH);
Uri yourUri = Uri.fromFile(f);

Upvotes: 23

Aurelian Cotuna
Aurelian Cotuna

Reputation: 3081

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 

Upvotes: 0

Related Questions