0

I use this code to achieve "Open in" function for File Class (java.io.File):

...
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(fileExtension);

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), type); // Standard File class            
startActivity(Intent.createChooser(intent, "blabla"));
...

I would like to do the same with DocumentFile class (android.support.v4.provider.DocumentFile):

...
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(fileExtension);

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(documentFile.getUri(), type); // DocumentFile class          
startActivity(Intent.createChooser(intent, "blabla"));
...

However, It seems no application (even on Android 5) is able to handle URI - documentFile.getUri().

Do I do anything wrong?

Quark
  • 1,578
  • 2
  • 19
  • 34

1 Answers1

3

This worked for me:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(docFile.getUri(), type);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
kuato
  • 46
  • 1
  • 3
  • Yes, it really works! With one bizarre exception regarding video files (there is no video, only audio track plays), however it can be related to who know what. Thanks! – Quark Mar 17 '15 at 13:53
  • You are welcome, I spent some time trying to make it work. I've only tried it with images, so no idea. – kuato Mar 17 '15 at 16:57