Search code examples
androidandroid-intentgalleryfilechooser

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 png 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.setType(mime);
intent.setData(Uri.fromFile(file));  // <----- png file
Intent chooser = Intent.createChooser(intent, "Choose an application to open with:");
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chooser);

Whats wrong here?


Solution

  • 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:");
    context.startActivity(chooser);
    

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