Android - How do I open a file in another app via Intent?

后端 未结 2 813
天命终不由人
天命终不由人 2021-02-15 07:48

I\'m trying to open a file using another app, i.e. opening a .jpg with Gallery, .pdf with Acrobat, etc.

The problem I\'m having with this is when I try to open the file

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-15 08:28

    I used this piece of code and it worked perfectly fine on android 6 and below not tested on the higher version

    public void openFile(final String fileName){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File(android.os.Environment.getExternalStorageDirectory()
                .getAbsolutePath()+ File.separator+"/Folder/"+fileName));
        intent.setDataAndType(uri, "audio/mpeg");
        startActivity(intent);
    }
    

提交回复
热议问题