Showing posts with label android share. Show all posts
Showing posts with label android share. Show all posts

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.



Thursday, 31 August 2017

Converting android View to Bitmap

Android View to Bitmap

Today we will learn how to convert the android view or XML to a bitmap. During android development sometimes we need to convert the view or xml to bitmap for different purposes. One use case could be to share the the bitmap using sharing intent provided by google. You can find the tutorial here how can you share the bitmap without saving it to external memory. Now let's get started with the tutorial to convert the xml to bitmap.

Implementation

Actually it is very easy to convert the xml to a bitmap. You can use the drawing class provided by the google android. 
The hack here is that as our view/xml is not visible, so it is most likely not inflated. What we have done is called the layout() and measure() methods of the view ourself which are actually called by the system when a view is set on the activity to set the width and height of the layout.

Calling This Method

We can call this method by passing our view in this method. But first we need to inflate that xml.
What we have done here is that inflating our xml are then passing it our method to create the bitmap from here. Pretty simple, right! That's it. You have successfully created the bitmap from xml. Happy Coding!! In case of query please comment below or contact me at farooqahmadkhan003@gmail.com.