Android Resource File Provider

Additional

Language
Java
Version
1.0.0 (Jan 26, 2019)
Created
Sep 14, 2018
Updated
Feb 6, 2019
Owner
Federico Boschini (federicoboschini)
Contributor
Federico Boschini (federicoboschini)
1
Activity
Badge
Generate
Download
Source code

Advertisement

Android Resource File Provider

Easily share audio, video, image and document files from raw, assets and drawable folders without any specific permission.

Installation

Include the dependency in your app build.gradle file:

implementation 'it.federicoboschini:resource-file-provider:1.0.0'

If you are experiencing any issue with resource merging or support-library compatibility add the dependency excluding com.android.support:

implementation('it.federicoboschini:resource-file-provider:1.0.0') {
    exclude group: 'com.android.support'
}

Configuration

Add this to your app AndroidManifest.xml:

<application>
<!--    ...     -->
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="@string/rfp_provider_authority"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_provider_paths" />
    </provider>
<!--    ...     -->
</application>

Optional Add this (provider authority) to your app strings.xml:

<resources>
    <!--    ...     -->
    <string name="rfp_provider_authority">it.my_company.my_app.my_authority</string>
    <!--    ...     -->
</resources>

Optional Define custom file paths by creating a file_provider_paths in res/xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!-- Define the desired file paths -->
    
    <!--    The default is:     -->
    <!-- <files-path name="all" path="/"/> -->
</paths>

Example: share a mp3 file

try {
    ResourceFileProvider.Builder
        .from(this)
        .setDirectory(FOLDER_RAW)
        .setFileName("my_sound")
        .setFileExtension("mp3")
        .setFileType(TYPE_AUDIO)
        .build()
        .shareFile();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Note: setFileName parameter is without the extension.

Current supported and tested extensions:

  • .mp3
  • .jpg
  • .png
  • .pdf
  • .mp4

Current supported and tested folders:

  • drawable
  • raw
  • assets

Note: if your project contains density-dependent drawables, the nearest to the device density will be selected.

Known issues:

  • Can't get images from mipmap folder.