Android 11 (R) file path access

After asking a question on issuetracker, I’ve come to the following conclusions:

  • On Android R, the File restrictions that were added in Android Q is removed. So we can once again access File objects.
  • If you are targeting Android 10 > and you want to access/use file paths, you will have to add/keep the following in your manifest:

    android:requestLegacyExternalStorage="true"
    

    This is to ensure that file paths are working on Android 10(Q). On Android R this attribute will be ignored.

  • Don’t use DATA column for inserting or updating into Media Store, use DISPLAY_NAME and RELATIVE_PATH, here is an example:

    ContentValues valuesvideos;
    valuesvideos = new ContentValues();
    valuesvideos.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "YourFolder");
    valuesvideos.put(MediaStore.Video.Media.TITLE, "SomeName");
    valuesvideos.put(MediaStore.Video.Media.DISPLAY_NAME, "SomeName");
    valuesvideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
    valuesvideos.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
    valuesvideos.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
    valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 1);
    ContentResolver resolver = getContentResolver();
    Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
    Uri uriSavedVideo = resolver.insert(collection, valuesvideos);
    
  • You can no longer use the ACTION_OPEN_DOCUMENT_TREE or the ACTION_OPEN_DOCUMENT intent action to request that the user select individual files from Android/data/,Android/obb/and all sub-directories.

  • It is recommended to only use File objects when you need to perform “seeking”, like when using FFmpeg, for example.
  • You can only use the data column to access files that are on the disk. You should handle I/O Exceptions accordingly.

If you want to access a File or want a file path from a Uri that was returned from MediaStore, I’ve created a library that handles all the exceptions you might get. This includes all files on the disk, internal and removable disk. When selecting a File from Dropbox, for example, the File will be copied to your applications directory where you have full access, the copied file path will then be returned.

Leave a Comment