Stralo
Stralo

Reputation: 474

intent.setDataAndType() not working on android 5

I'm trying to pick an image from a specific folder using intent in the following manner:

private void selectPicture(){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/Pictures/");
    intent.setDataAndType(uri, "image/*");
    startActivityForResult(intent, 1);
}

The image chooser is launched correctly. However, I see all the images (both those from my specified directory and those from other locations on the phone) instead of those from the specified directory only.

I'd appreciate any tips on how to get this to work.

I moved the app's images to the Pictures directory only because I couldn't get to show those in the app's private externalFilesDir(). If you know how to get this functionality to work for images in the app's external files directory, I'd really appreciate that as well.

Thanks!

Upvotes: 0

Views: 657

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007296

ACTION_GET_CONTENT was never designed to use a Uri, and it is certainly not documented to only allow the user to choose "those from the specified directory only". Quoting the documentation for ACTION_GET_CONTENT:

Note that no URI is supplied in the intent, as there are no constraints on where the returned data originally comes from.

You are welcome to create your own content browsing UI that constrains the user to a particular directory, perhaps using MediaStore to find the relevant media to allow the user to choose from.

Also, on Android 4.4+, you could use the Storage Access Framework to allow the user to pick content. There may be a flow that allows you to programmatically "pick" the directory, then use that as the basis for the system to allow the user to pick content in that directory. However, I have not seen this done -- usually, the user gets to start at the root of all available storage providers.

Upvotes: 6

Related Questions