Launching an intent for file and MIME type?

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.

Leave a Comment