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

后端 未结 9 1919
梦谈多话
梦谈多话 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条回答
  •  Happy的楠姐
    2020-12-02 18:17

    Try the below code. I am using this code for opening a PDF file. You can use it for other files also.

    File file = new File(Environment.getExternalStorageDirectory(),
                         "Report.pdf");
    Uri path = Uri.fromFile(file);
    Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
    pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    pdfOpenintent.setDataAndType(path, "application/pdf");
    try {
        startActivity(pdfOpenintent);
    }
    catch (ActivityNotFoundException e) {
    
    }
    

    If you want to open files, you can change the setDataAndType(path, "application/pdf"). If you want to open different files with the same intent, you can use Intent.createChooser(intent, "Open in...");. For more information, look at How to make an intent with multiple actions.

提交回复
热议问题