Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

android - Launching an intent for file and MIME type?

I've reviewed all the similar questions here, but I can't for the life of me figure out what I'm doing wrong.

I've written an application that tries to launch various files, sort of a file browser. When a file is clicked, it tries to launch the program based on its associated MIME type or it presents the "Choose Application to Launch" dialog.

Here's the code I'm using to launch:

    File file = new File(app.mediaPath() + "/" +_mediaFiles.get(position));

    Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);

    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    myIntent.setDataAndType(Uri.fromFile(file),mimetype);
    startActivity(myIntent);

This fails and generates the error:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///file:/mnt/sdcard/roms/nes/Baseball_simulator.nes }

Now if I install OI File Manager for instance, it opens instead of this error being thrown, and then if I click the same file from within in it, it launches the approriate dialogs.

I have noticed that the MIME type for that particular file fails, but other mime types like .zip do return values.

Am I missing something that when the MIME type is null to call a dialog that lets the user select?

I've tried other variations of launching the app, including not setting the MIME type and only using .setData with no success.

The action I want to happen is, a user clicks a file, if it's associated with an application that app launches, if not, the user gets the "Complete action using" dialog with a list of apps.

Thanks for any advice.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well thanks to the Open Intent guys, I missed the answer the first time through their code in their file manager, here's what I ended up with:

    File file = new File(filePath);
    MimeTypeMap map = MimeTypeMap.getSingleton();
    String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
    String type = map.getMimeTypeFromExtension(ext);

    if (type == null)
        type = "*/*";

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data = Uri.fromFile(file);

    intent.setDataAndType(data, type);

    startActivity(intent);

If you use a mime type of "* / *" when you can't determine it from the system(it is null) it fires the appropriate select application dialog.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...