Launching an intent for file and MIME type?

前端 未结 2 376
孤独总比滥情好
孤独总比滥情好 2020-12-02 17:45

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 va

2条回答
  •  春和景丽
    2020-12-02 18:24

    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.

提交回复
热议问题