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

后端 未结 9 1910
梦谈多话
梦谈多话 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 18:12

    Try this one add this code to your manifest file

     
                
            
    

    provide your path type path.xml

    
        
    
    

    and add this code to your functionality

    File file = new File(tempPathNameFileString);
    Intent viewPdf = new Intent(Intent.ACTION_VIEW);
    viewPdf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    Uri URI = FileProvider.getUriForFile(ReportsActivity.this, ReportsActivity.this.getApplicationContext().getPackageName() + ".provider", file);
    viewPdf.setDataAndType(URI, "application/pdf");
    viewPdf.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    ReportsActivity.this.startActivity(viewPdf);
    

提交回复
热议问题