android: open a pdf from my app using the built in pdf viewer

前端 未结 9 1730
温柔的废话
温柔的废话 2020-11-29 04:46

This was my original question:

I want to be able to open a pdf file in my app using the android\'s built in pdf viewer app, but i dont know how to

相关标签:
9条回答
  • 2020-11-29 05:19

    Add FLAG_GRANT_READ_URI_PERMISSION

    Intent intent = new Intent(Intent.ACTION_VIEW)
    Uri outputFileUri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file); 
    intent.setDataAndType(outputFileUri, "application/pdf"); 
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    Intent in = Intent.createChooser(intent, "Open File");
    startActivity(in);
    

    also add provider_paths.xml at res -> xml folder and need to add below code at manifests

    <application>
       <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="${applicationId}.provider"
                android:exported="false"
                android:grantUriPermissions="true"
                tools:replace="android:authorities">
                <meta-data
                    tools:replace="android:resource"
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/provider_paths" />
            </provider>
     </application>
    
    0 讨论(0)
  • 2020-11-29 05:20

    AFAIK, Adobe has not documented any public Intents it wants developers to use.

    You can try an ACTION_VIEW Intent with a Uri pointing to the file (either on the SD card or MODE_WORLD_READABLE in your app-local file store) and a MIME type of "application/pdf".

    0 讨论(0)
  • 2020-11-29 05:25
                FileFinalpath = SdCardpath + "/" + Filepath + Filename;
                File file = new File(FileFinalpath);
                if (file.exists()) {
                    Uri filepath = Uri.fromFile(file);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(filepath, "application/pdf");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
                    try {
                        startActivity(intent);
                    } catch (Exception e) {
                        alert.showAlertDialog(PDF_Activity.this, "File Not Started...","File Not Started From SdCard ", false);             
                        Log.e("error", "" + e);
                    }
    
                } else {
                    alert.showAlertDialog(PDF_Activity.this, "File Not Found...","File Not Found From SdCard ", false);             
    
                }
    
    0 讨论(0)
提交回复
热议问题