Android Scoped Storage (10+) Cross-Profile Pick From Gallery

Viewed 107

With the recent Google Play ruling which have to remove the legacy storage access if targeting 11+.

I couldn't find a way for cross-profile to work. My current code works just fine using the legacy storage access - able to pick media from work profile to personal profile. When I remove the android:requestLegacyExternalStorage=true from AndroidManifest.xml, my current codebase is no longer able to see media from the personal profile.

My current intent is as simple as follow which works if within same profile but not for cross-profile.

My question is: What am I missing in order to make it work? The code snippet posted below works fine within same profile. But the moment it goes cross-profile, it stops working.

Devices used: Xiaomi Pocophone F1, Samsung Galaxy A51

val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
activity.startActivityForResult(Intent.createChooser(intent, "Add From Gallery"), 100)
1 Answers

If cross-profile refers to additional users added in android using a work profile, etc, The following would be helpful :

The work profile and the personal profile use separate storage areas, a file URI that is valid on one profile is not valid on the other. This situation means that if you attach a file URI to an intent, and the intent is handled on the other profile, the handler is not able to access the file.

Instead, you should share files with content URIs as demonstrated :


// Open File object from its file URI
File fileToShare = new File(fileUriToShare);

Uri contentUriToShare = FileProvider.getUriForFile(getContext(),"com.example.myapp.fileprovider",fileToShare);

The content URI contains the file path, but also the authority that provides the file, and an ID number identifying the file. You can generate a content ID for any file by using a FileProvider. You can then share that content ID with other apps (even on the other profile). The recipient can use the content ID to get access to the actual file.

Source: https://developer.android.com/work/managed-profiles#sharing_files

Related