I have written a piece of code that detects installed application in android and opens the file with the application.For example, a word document should be opened with some office apk.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(temp_file);
String type = getMimeType(temp_file.getName());
intent.setDataAndType(data, type);
this.startActivity(intent);
In the above code temp_file is the file that should be opened.And below is the generalised code that I wrote to get the MIME type
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
return type;
}
But when I execute ,it throws android.content.ActivityNotFoundException exception.So,am I doing anything wrong here?
You are calling
getMimeType()and passing it the file name. But your methodgetMimeType()expects an URL. The documentation forMimeTypeMap.getFileExtensionFromUrl()specifically says:This method is a convenience method for obtaining the extension of a url and has undefined results for other Strings.
You are probably getting
nullfor the mime type. Add some debug logging and check whatgetMimeType()is returning.Also, look in the logcat. It should tell you the content of the
Intentit is trying to resolve. That should also give you a hint.