How to open a file in Android (for example, an image) by means of the system. That is, to open the file in the application that is provided for this type.
Updated.
In order for the system to understand which applications it can open the file, you must explicitly specify the mime-type. The library has a special class for working with mime-types: android.webkit.MimeTypeMap
.
In order to get the mime-type, we first find out the file extension.
String extension = MimeTypeMap.getFileExtensionFromUrl(url); String mType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
But such code will not work, since
- The
getFileExtensionFromUrl
method returns an extension true, only if the file name contains only Latin characters (no spaces, no Cyrillic, etc.). - The
getMimeTypeFromExtension
method works correctly only if the extension is in lower case (so addtoLowerCase
).
Further. If you configure intent like this
intent.setData(Uri.fromFile(file)); intent.setType(mType);
then only type
will be set, and data
will be null
. If the lines are reversed, then, on the contrary, type == null
. Those. either one or the other. It only worked like this:
intent.setDataAndType(Uri.fromFile(file), mType);
(tested on a device from Motorola)