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

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

    To Open a File in Android Programatically,you can use this code :- We use File Provider for internal file access .You can also see details about File Provider here in this linkfileprovidr1,file provider2,file provider3. Create a File Provider and defined in Manifest File .

    
    
        
        
    
    
    

    Define file_path in resources file.

    
    
        
    
        
        
        
        
        
        
    
    
    

    Define Intent For View

    String directory_path = Environment.getExternalStorageDirectory().getPath() + "/MyFile/";
    String targetPdf = directory_path + fileName + ".pdf";
    File filePath = new File(targetPdf);
    
    Intent intentShareFile = new Intent(Intent.ACTION_VIEW);
    File fileWithinMyDir = new File(targetPdf);
    Uri bmpUri = FileProvider.getUriForFile(activity, "com.packagename.app.fileprovider", filePath);
    if (fileWithinMyDir.exists()) {
        intentShareFile.setDataAndType(bmpUri,"application/pdf");
        intentShareFile.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intentShareFile.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(Intent.createChooser(intentShareFile, "Open File Using..."));
    
    }
    

    You can use different way to create a file provider in android . Hope this will help you.

提交回复
热议问题