Open a selected file (image, pdf, …) programmatically from my Android Application?

后端 未结 9 1950
梦谈多话
梦谈多话 2020-12-02 17:34

I\'m working on an Android application which should be able to open a selected file from a specific folder.

I already tried this, but after selecting which applicati

9条回答
  •  没有蜡笔的小新
    2020-12-02 17:55

    You can try this

    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);
    

提交回复
热议问题