I try to export a bitmap from my app using share intent without saving a file for a temporal location. All the examples I found are two-step 1) save to SD Card and create Uri for that file 2) start the intent with this Uri
Is it possible to make it without requiring WRITE_EXTERNAL_STORAGE permission, saving the file [and removing it afterwards]? How to address devices without ExternalStorage?
If anyone still looking for easy and short solution without any storage permission (Supports nougat 7.0 as well). Here it is.
Add this in Manifest
Now create provider_paths.xml
Finally Add this method to your activity/fragment (rootView is the view you want share)
Here is working method to make a screenshot of own app and share it as image via any messanger or email client.
To fix the bitmap not updating problem I improved Suragch's answer, using Gurupad Mamadapur's comment and added own modifications.
Here is code in Kotlin language:
This for sharing CardView as an Image then saving it in the cache subdirectory of the app's internal storage area. hope it will be helpful.
I had this same problem. I didn't want to have to ask for the read and write external storage permissions. Also, sometimes there are problems when phones don't have SD cards or the cards get unmounted.
The following method uses a ContentProvider called FileProvider. Technically, you are still saving the bitmap (in internal storage) prior to sharing, but you don't need to request any permissions. Also, every time you share the bitmap the image file gets overwritten. And since it is in the internal cache, it will be deleted when the user uninstalls the app. So in my opinion, it is just as good as not saving the image. This method is also more secure than saving it to external storage.
The documentation is pretty good (see the Further Reading below), but some parts are a little tricky. Here is a summary that worked for me.
Set up the FileProvider in the Manifest
Replace
com.example.myapp
with your app package name.Create res/xml/filepaths.xml
This tells the FileProvider where to get the files to share (using the cache directory in this case).
Save the image to internal storage
Share the image
Further reading
In theory, this is possible. In practice, it is probably not possible.
In theory, all you need to share is a
Uri
that will resolve to the bitmap. The simplest approach is if that is a file that is directly accessible by the other application, such as on external storage.To not write it to flash at all, you would need to implement your own
ContentProvider
, figure out how to implementopenFile()
to return your in-memory bitmap, and then pass aUri
representing that bitmap in theACTION_SEND
Intent
. SinceopenFile()
needs to return aParcelFileDescriptor
, I don't know how you would do that without an on-disk representation, but I have not spent much time searching.If you simply do not want it on external storage, you can go the
ContentProvider
route, using a file on internal storage. This sample project demonstrates aContentProvider
that serves up a PDF file viaACTION_VIEW
to a PDF viewer on a device; the same approach could be used forACTION_SEND
.