In my android app, I have a bitmap (say b) and a button. Now when I click on the button, I want to share the bitmap. I am making use of the below code inside my onClick()
to achieve this :-
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, b);
startActivity(Intent.createChooser(intent , "Share"));
I was expecting a list of all application which are able to handle this intent but I get nothing. There is no list of apps nor is there any error in android studio. My application just get hanged for sometime and then quits.
I have checked the bitmap and it is fine (its not null).
Where am I a going wrong ?
As CommonsWare stated you need to get the URI to the bitmap and pass that as your Extra.
Quoting the documentation:
b
, therefore, is not supposed to be aBitmap
, but rather aUri
pointing to aBitmap
, served by aContentProvider
. For example, you could write theBitmap
to a file, then useFileProvider
to serve it.I found out 2 variants of the solution. Both are saving Bitmap to the storage, but the image is not showed in the gallery.
First variant:
Saving to external storage - but to private folder of the app. - for API <= 18 it requires permission, for newer it does not.
1. Setting permissions
Add into AndroidManifest.xml before tag
2. Saving method:
3. Checking storage accesibility
External storage might not be accessible, so you should check it before trying to save - https://developer.android.com/training/data-storage/files
Second variant
Saving to cacheDir using FileProvider. It does not require any permission.
1. Setup FileProvider in AndroidManifest.xml
2. Add path to the res/xml/file_paths.xml
3. Saving method:
More info about file provider - https://developer.android.com/reference/android/support/v4/content/FileProvider
Compressing and saving can be time-consuming and should be done in different thread
Actual sharing
After spending a lot of time on this:
Check if permissions are given. Then:
Step 1: Create ImageView of the image you want to in the activity and then convert it itno bitmap
Step 2: Store the image in internal folder:
Step 3: Send the saved image:
Now after sending you can delete the saved image if you don't want it in your storage. Check other link to do that.
** finally i got the solution.**
Step 1 : Share Intent handling Block. This will Pop Up your window with list of Applications in you phone
Step 2 : Converting your view to BItmap
Step 3 :
To get The URI from Bitmap Image