Sunday 10 September 2017

Android Sharing Image Using Share Intent

Today I am going to write about how you can share images or files or other content without saving them to external memory. YES, WITHOUT SAVING THEM TO EXTERNAL MEMORY. This will save you from a lot of unnecessary code and permission in you application.  The hack here is we will save them to cache and then share it. If you want to share you views/xml see the article here which explains how you can convert your view to bitmaps.

Why Avoid Using External Storage

Alot of users not do not grant permission for reading and writing of external storage due to some security concerns. And moreover why use external storage when you can do a task without using external storage. It is also valid point that caches are much much faster then the external storage. It will speed up the sharing process of your application.

Steps for sharing Without Saving to External Memory

We can share our content by following the simple three steps.
  • Add the provider tag in your AndroidManifest.xml file.
  • Defile the filepaths.xml file in xml directory.
  • Save the image in the cache and then share it.

Method to Share Images

Add this code in your application tag in your Androidmanifest.xml file. Here com.vd.viewtobitmap is my package name. Replace it with your package name and append it with .fileprovider.
Now you need to do one more thing, that is defining the paths for images to save them. You can do it by creating a filepaths,xml file in xml directory within your res folder.
Here is the method you can use to share your image files without saving them to external storage. You only need a little changes to make it work for the file extension you want. Learn how can you create the bitmap to pass it in this function.
What we have done here through a little hack is saving the bitmap which is passed in the function to the cache. And then reading the file URI for sharing and granting temporary permission for accessing this file for sharing. Pretty simple, right! That's it. You have successfully shared the file/image without saving it to external memory. Happy Coding!! In case of query please comment below or contact me at farooqahmadkhan003@gmail.com.



3 comments: