Running file based operation in android

I am developing an application that lists the files in a folder (in a ListView). When the user clicks on one of the elements, if it is a file, I would like to launch an action that can handle it if it exists, or display some kind of error message if it is not there.

How can i do this? Of course, not all, but how can I determine which applications can process the file, if any.

+5
source share
2 answers

First you need to determine the type of MIME file. You can do this using MimeTypeMap :

MimeTypeMap map = MimeTypeMap.getSingleton();
String extension = map.getFileExtensionFromUrl(url); // url is the url/location of your file
String type = map.getMimeTypeFromExtension(extension);

, MIME, :

Intent intent = new Intent();
intent.setType(type);

, , - . PackageManager:

PackageManager manager = getPackageManager(); // I'm assuming this is done from within an activity. This a Context method.
List<ResolveInfo> resolvers = manager.queryIntentActivities(intent, 0);
if (resolvers.isEmpty()) {
  // display error
} else {
  // launch the intent. You will also want to set the data based on the uri of your file
}
+7

All Articles