Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to get a content:// URI for a file in the external storage PUBLIC directory

Tags:

I've followed this Google tutorial to start an intent to capture an image with new Intent(MediaStore.ACTION_IMAGE_CAPTURE). The tutorial recommends using the public directory with getExternalStoragePublicDirectory which would work great for my app. But then their example instead uses getExternalFilesDir. Then to pass the URI of the file to the intent with MediaStore.EXTRA_OUTPUT I have to get a content URI because I'd like to target Android N. Before targeting N I would just pass a file:// URI and everyone but Google was happy. Now on N I started getting the FileUriExposedException that seems to be not very favorable.

So given that I have a File like this...

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyAppFolder");
    if (!storageDir.exists() && !storageDir.mkdir())
        Log.w(TAG, "Couldn't create photo folder: " + storageDir.getAbsolutePath());
    File image = new File(storageDir, timeStamp + ".jpg");
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

...can I use a built-in provider for the public pictures directory to get a content URI? If so how?

I've tried something like

takePictureIntent.putExtra(
    MediaStore.EXTRA_OUTPUT, 
    FileProvider.getUriForFile(this, MediaStore.AUTHORITY, createImageFile()));

but it just throws

IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data

Is that Authority correct? If I must use my own provider to share the public file then what path can I specify in my FILE_PROVIDER_PATHS meta-data? All of the options I can find are for private directories.

like image 405
Paul Avatar asked Jun 21 '16 19:06

Paul


People also ask

How do I find the URI of a file?

In general, an URI (Uniform Resource Identifier) represents a resource. The URI class of Java represents this format You can get the URI format of a file by invoking the toURI() method.

What is content Uri Android?

A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table (a path). When you call a client method to access a table in a provider, the content URI for the table is one of the arguments.


1 Answers

TL:DR <external-path name="MyAppFolder" path="." /> works, but is it safe?

You will need to make your own FileProvider. It's worth reading its documentation, but here's a brief rundown.

As @CommonsWare mentioned, you'll need to replace MediaStore.AUTHORITY from your example with "com.example.fileprovider", which is the authority you will define in your manifest like so...

Within the <application> tag in the AndroidManifest, type the following:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

The exception you're getting is due to not having that <meta-data> tag that links to an xml file with all the paths that your FileProvider is allowed to touch. But it's what goes in this file that I've been struggling with myself.

Towards the end of the tutorial you linked, at the end of the Save the Full-size Photo section, the example Google gives for this file_paths.xml is:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>

"my_images" would be replaced with "MyAppFolder" in your example. In that tutorial Google claims

The path component corresponds to the path that is returned by getExternalFilesDir() when called with Environment.DIRECTORY_PICTURES. Make sure that you replace com.example.package.name with the actual package name of your app.

...and typing that out made me realize that that is NOT referring to the public Pictures directory that you and I are looking for. Which explains why using that path produces this exception:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Pictures/MyAppFolder/{filename}.jpg

I'll leave that in here for future reference, and move on to answering your big question--what path can you specify to allow other apps to interact with a file in the public Pictures directory created by your app targeting API level 24+?

As @CommonsWare said, "the details of your desired subdirectory vary by device" so you can't declare the path to the public Pictures directory, but I did find a way that works.

<external-path name="MyAppFolder" path="." /> will allow your FileProvider to give a content URI to other apps (like the Camera), which can then read and write to the file it resolves to.

If there are any dangers or downsides to this, I would love to hear them.

like image 65
pumpkinpie65 Avatar answered Oct 03 '22 02:10

pumpkinpie65