Thursday, November 16, 2017

FileProvider in Android Nougat

FileProvider is a special sub-class of ContentProvider class helps in secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file ://Uri

A content Uri allows you to grant read and write access using temporary access permission. When you create an intent containing a content URI, in order to send the content URI to client app, you can also call intent.setFlags() to add permissions. This permissions are available to client app as long as the stack for a receiving Activity is active. For an intent going to a service , the permissions are available as long as the service is running


 Defining a FileProvider

Since the default functionality of FileProvider includes content uri generation for the file, you don't need to define a subclass in code. Instead you can include a fileprovider in your app by specifying it entirely in XML. 


<manifest>
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mydomain.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            ...
        </provider>
        ...
    </application>
</manifest>
If you want to override any of the default behaviour of FileProvider methods, extend the FileProvider class and use the fully qualified class name in the
android:name attribute of the <provider> element

android:authorities > set to a URI authority based on domain you control;
if you use the same authorities for different apps, while installing it may through an exception to uninstall existing app which has same authority

android:exported > set to false, File provider does not need to be public

android:grantUriPermissions > set to true, to allow you to temporary access to files.



Specifying Available Files

nt A file provider can only generate content URI for files in directories that you specify beforhand
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_images" path="images/"/>
    ...</paths>

Related Posts:

  • Android Test4 1.  What does the Gargenta mean in his Design Philosophy when he says that the project will, “Always be whole and complete”?   … Read More
  • Permission requests in runtime of application As everyone knows Google as changed the permission access for android applications since Android API level >22, So Google suggested to grant permi… Read More
  • Migrating Apps to Android O Google has taken the development world by surprise by announcing a developer release of Android O. While speculations are rife about what sweet the … Read More
  • Add XML fonts in Android O As we all know Android has released its Android O preview for developers,now developer can download this preview version on there Android Studio vers… Read More
  • Android Test3 1.  What is a funny fact about the start of Android?   A )   It was orginaly going to be called UFO   … Read More

0 comments:

Post a Comment