Skip to main content

Sharing Files With Android Apps

There are times when you have to share one or more files with other apps? Let’s take an example. You might have to send images to image editors. On the other hand, you might also Copy & Paste files between external storage? The best way, in this case, is to send the file’s content URI to the receiving app. You can allow the receiving app with temporary access permissions to the file’s content URI.

Why is Content URI safe?

The reason for this is that the content URI only applies to the app that receives the URI.  Once that has been done, it expires automatically.   There are ways of generating a file’s content URI.  One of them is the getUriForFile() method which is offered by Android FileProvider component.
In this article, we will talk about ways of securing sharing files between apps by using content URIs and temporary permissions. The content URIs will be generated by using the Android FileProvider component.

1) Set up file sharing

The first thing that you need to do in the process to configure the app to offer a secure way of file handling. This way will be in the form of Content URI.
As we have discussed earlier, FileProvider component is a great way of creating content URIs. It performs the specified task on the basis of the specifications you provide in XML. Let us spend some time to explore ways of adding the default implementation of FileProvider. We will also look at ideas of pointing to the files that have to be shared with other apps.
Steps:
  1. Specify the FileProvider
  2. Specify Sharable Directories
NOTE: FileProvider class is an element of the v4 Support Library.

1.1) Specify the FileProvider

To be able to use FileProvider for the app, you need to have an entry in the manifest.  What does this entry do?  There are a couple of things it specifies including authorizing its use for creation of content URIs and also specifies the XML file’s name which is used to specify the directories that the app can share.
As you can see in provided code snippet, you need to add the element of <provider> in order to specify the FileProvider class as also the authority.
android:authorities attribute is used to specify the URI authority.  This is meant for those content URIs that are generated by the FileProider.  In the provide code snippet, the authority is com.example.demoapp.fileprovider
<meta-data> is the child element of the <provider> element , it points to an XML file that specifies the directories we want to share.
For the specification of the file’s name and path, the android: resource attribute is used.  This is sans the .xml extension.

Comments

Popular posts from this blog

Handle Scrollable Views or Controls in another Scrollview in Android

In life of any Android developer, there are times when there is a requirement in the application to use one scrollable control inside another scrollable control. In such cases, the scrolling functionality of one or all scrollable controls gets enabled and the Android application cannot identify that scroll event of which control is to be handled first. Here, UI may get stuck in some scenarios. The ideal behavior expected is when the user touches the parent scroll view, only the parent view’s scroll event is called and when the user scrolls the child scroll view, the child view’s scroll event is called. In our standard Android application development and implementation process it does not happen when we add scrollable control in another scrollable control. Below is the solution to handle the scroll event of both the controls without any glitch. Example We have a layout that has a ScrollView as parent layout and it contains some TextViews, ImageViews and ListView as child control...
Android Push Notification Using Firebase Cloud Messaging In this tutorial, I’m gonna show you how to use the push notifications in your android applications. Before the Google I/O 2016 , we were using the Google Cloud Messaging service (GCM) to send the data from the server to the clients or the android power devices but in I/O 2016, Google introduced the Firebase Cloud Messaging which is a good alternative and easier to implement. Steps to implement FireBase push notification in Android 1: Import the code of FCM   https://drive.google.com/a/edreamz.in/file/d/0B1L0RecNhNk9UDhhOXRZQjJYU0k/view?usp=sharing 2: go to    https://console.firebase.google.com/  and create a new project. 4: Now put your app name and select your country. 5: Now click on Add Firebase to Your Android App. 6: Now you have to enter your projects package name and click on ADD APP. 7:After clicking add app you will get google-services.json fil...
Android Push Notification Using Firebase Cloud Messaging In this tutorial, I’m gonna show you how to use the push notifications in your android applications. Before the Google I/O 2016 , we were using the Google Cloud Messaging service (GCM) to send the data from the server to the clients or the android power devices but in I/O 2016, Google introduced the Firebase Cloud Messaging which is a good alternative and easier to implement. Steps to implement FireBase push notification in Android 1: Import the code of FCM   https://drive.google.com/a/edreamz.in/file/d/0B1L0RecNhNk9UDhhOXRZQjJYU0k/view?usp=sharing 2: go to    https://console.firebase.google.com/  and create a new project. 4: Now put your app name and select your country. 5: Now click on Add Firebase to Your Android App. 6: Now you have to enter your projects package name and click on ADD APP. 7:After clicking add app you will get google-services.json fil...