Lau Lau - 1 year ago 79
Android Question

Android Intent chooser - wrong propositions

I want to open any file in my app using intent chooser - unfortunately, propositions from chooser are... weird. After choosing

file it displays only one option - Drive PDF Viewer. But when I try to open the same file using default files explorer it correctly recommends using Galery or Photos.

String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext.toLowerCase());
Log.i(TAG, mime); // <------------ image/png
Intent intent= new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(file)); // <----- png file
Intent chooser = Intent.createChooser(intent, "Choose an application to open with:");

Whats wrong here?

Answer Source

Try this with setDataAndType:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "image/*");
Intent chooser = Intent.createChooser(intent, "Choose an application to open with:");

Please see the Javadoc of the method setType(String type) in the class Intent:
This method automatically clears any data that was previously set (for example by setData(Uri)).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download