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.
Here is what I'm using in my app for Pictures public directory.
Create the file like this:
Specify the following path in file provider's meta data:
"secure_name" is used to hide the name of the subdirectory ("YOUR_APP_NAME" in this case) you're sharing for security reasons.
"Pictures" corresponds to
Environment.DIRECTORY_PICTURES
.Obtain
Uri
to send withIntent
: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: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 yourFileProvider
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:"my_images"
would be replaced with"MyAppFolder"
in your example. In that tutorial Google claims...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:
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 yourFileProvider
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.