PublicFileProvider

Android library to publicly expose files via content:// URI. Consider using FileProvider instead!

License

License

Categories

Categories

IDE Development Tools
GroupId

GroupId

de.cketti.fileprovider
ArtifactId

ArtifactId

public-fileprovider
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

aar
Description

Description

PublicFileProvider
Android library to publicly expose files via content:// URI. Consider using FileProvider instead!
Project URL

Project URL

https://github.com/cketti/PublicFileProvider
Source Code Management

Source Code Management

https://github.com/cketti/PublicFileProvider

Download public-fileprovider

How to add to project

<!-- https://jarcasting.com/artifacts/de.cketti.fileprovider/public-fileprovider/ -->
<dependency>
    <groupId>de.cketti.fileprovider</groupId>
    <artifactId>public-fileprovider</artifactId>
    <version>1.0.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/de.cketti.fileprovider/public-fileprovider/
implementation 'de.cketti.fileprovider:public-fileprovider:1.0.0'
// https://jarcasting.com/artifacts/de.cketti.fileprovider/public-fileprovider/
implementation ("de.cketti.fileprovider:public-fileprovider:1.0.0")
'de.cketti.fileprovider:public-fileprovider:aar:1.0.0'
<dependency org="de.cketti.fileprovider" name="public-fileprovider" rev="1.0.0">
  <artifact name="public-fileprovider" type="aar" />
</dependency>
@Grapes(
@Grab(group='de.cketti.fileprovider', module='public-fileprovider', version='1.0.0')
)
libraryDependencies += "de.cketti.fileprovider" % "public-fileprovider" % "1.0.0"
[de.cketti.fileprovider/public-fileprovider "1.0.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

PublicFileProvider

PublicFileProvider is a special subclass of ContentProvider that facilitates exposing files associated with an app by creating a content:// URI for a file instead of a file:/// URI.

WARNING: Most of the time this is NOT what you want. Use FileProvider to only grant temporary access to files, e.g. when sharing content to other apps.

PublicFileProvider is a modified version of FileProvider with the specific goal to expose files without using Android's URI permission mechanism. This can come in handy when you have to provide a content:// URI but can't easily grant read access to whatever app ends up accessing the content. One use case is a custom ringtone in a notification. Check out the blog post Notifications, Sounds, Android 7.0, and Aggravation for more details. I also wrote a bit about how this library came to be: When URI permissions are in the way

Usage

Add a provider element to your Manifest:

<manifest>
    ...
    <application>
        ...
        <provider
            android:name="de.cketti.fileprovider.PublicFileProvider"
            android:authorities="com.mydomain.publicfileprovider"
            android:exported="true">
            
            <meta-data
                android:name="de.cketti.fileprovider.PUBLIC_FILE_PROVIDER_PATHS"
                android:resource="@xml/publicfileprovider_paths" />
        
        </provider>
        ...
    </application>
</manifest>

Create a file res/xml/publicfileprovider_paths.xml with the configuration, e.g.

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_notification_sounds" path="notification_sounds/"/>
</paths>

The format of this file is identical to that of FileProvider.

To get the content:// URI for a file you want to expose to all apps on the device use the following code:

File notificationSoundsPath = new File(Context.getFilesDir(), "notification_sounds");
File myNotificationSoundFile = new File(imagePath, "ding.ogg");
Uri contentUri = getUriForFile(getContext(), "com.mydomain.publicfileprovider", myNotificationSoundFile);

Include the library

The library is available on Maven Central. Add this to your dependencies block in build.gradle:

compile 'de.cketti.fileprovider:public-fileprovider:1.0.0'

License

Copyright 2016 cketti

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Versions

Version
1.0.0