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

后端 未结 2 945
眼角桃花
眼角桃花 2020-12-15 18:58

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 p

相关标签:
2条回答
  • 2020-12-15 19:26

    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.

    0 讨论(0)
  • 2020-12-15 19:31

    Here is what I'm using in my app for Pictures public directory.

    1. Create the file like this:

      File theFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "YOUR_APP_NAME" + File.separator + "YOUR_FILE_NAME");
      
    2. Specify the following path in file provider's meta data:

      <paths>
          <external-path name="secure_name" path="Pictures/YOUR_APP_NAME" />
      </paths>
      

    "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.

    1. Obtain Uri to send with Intent:

      final Uri theUri = FileProvider.getUriForFile(activity,
                          getString(R.string.fileprovider_authorities),
                          theFile);
      theIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
      theIntent.putExtra(MediaStore.EXTRA_OUTPUT, theUri);
      
    0 讨论(0)
提交回复
热议问题