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

后端 未结 9 1938
梦谈多话
梦谈多话 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:56

    MimeTypeMap.getSingleton().getExtensionFromMimeType(file.getName());
    

    Probably, this is the easiest solution.

    https://developer.android.com/reference/android/webkit/MimeTypeMap

    https://developer.android.com/reference/java/net/URLConnection.html#guessContentTypeFromName(java.lang.String)

    private void openFile(File file) {
    
        Uri uri = Uri.fromFile(file);
    
        Intent intent = new Intent(Intent.ACTION_VIEW);
    
        intent.setDataAndType(uri, MimeTypeMap.getSingleton().getExtensionFromMimeType(file.getName()));
    
    
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, "Open " + file.getName() + " with ..."));
    }
    

提交回复
热议问题