Search code examples
androidandroid-intentandroid-imageandroid-bitmapandroid-implicit-intent

Android: Does a bitmap created by my app have to be stored and scanned before I can pass it as an implicit intent to other apps?


Introduction and background

Hi, I have a bitmap created by my application, I wish for this bitmap to be sent to any apps that can handle the mime type "image/png" and the action ("ACTION_SEND"). Basically, allow the bitmap image to be sent to someone by another component like email and messaging etc.

What I got now

At moment my app saves the bitmap image to external memory within a directory I made. Each bitmap saved is scanned by the MediaScannerConnection and using the OnScanCompleteListener, I use the obtained Uri to build the implicit intent. This works fine.

My question

I am trying to cater for users who do not have external memory or have the setting ticked where they do not want any images saved/stored (just for that instance and gone when they close the app). So do I need to actually save an image in memory and get a Uri in order to pass a bitmap image as a implicit intent.

Many thanks for reading.


Solution

  • No, you dont need to save that image anywhere. Just get the filepath from the Bitmap:

    String path = Images.Media.insertImage(context.getContentResolver(), bitmap,"abcd", null);
    

    then get a valid Uri from it:

    Uri image = Uri.parse(path); 
    

    and add this Uri to your Intent like this:

    intent.putExtra(Intent.EXTRA_STREAM,  image);
    

    Done!