Cannot open PDF file in external app

Third-party apps cannot access your assets via file:///android_asset URLs.

You can:

  • Try using my StreamProvider, to have a ContentProvider in your app that can serve the PDF straight from assets, or

  • Use FileProvider from the Android Support package, after copying the file from assets to internal storage, as is demonstrated in this sample app, or

  • Copy the file to external storage and use a Uri created from Uri.fromFile() to point to that copy


Add this Permission and check again :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

it seems, there is a problem with accessing to the file.

Try to open another pdf file and let us know what's the Log.

and also i think you cannot use this : file:///android_asset

and take a look at this :

Choose the file with ExternalStorageDirectory:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");

From : https://stackoverflow.com/a/17453242/4945820

With Assets Folder:

and here is the code, if you are using this with Assets Folder!

File file = new File("file:///android_asset/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

and then add above permission and check it again with Assets code. Also, check this: https://stackoverflow.com/q/28032029/4945820


For newer API you can open PDF in WebView, see Load PDF file in webview. I tested, on API 21 device it offered several applications to open, in API 27 it opened inside WebView.

For usual opening in external reader copy this code. I used: intent.resolveActivity != null but launching the intent throws an ActivityNotFound exception, https://stackoverflow.com/a/57141679/2914140. You should define FileProvider first.

// Try to open PDF and return false if it is not possible.
fun openPdf(file: File, context: Context): Boolean {
    val uri = getUriFromFile(file, context)
    if (uri == null) {
        return false
    } else {
        val intent = Intent(Intent.ACTION_VIEW).apply {
            setDataAndType(uri, "application/pdf")
            flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        }
        // Validate that the device can open your File.
        val activityInfo = intent.resolveActivityInfo(context.packageManager, intent.flags)
        return if (activityInfo?.exported == true) {
            context.startActivity(Intent.createChooser(intent, "Open PDF")))
            true
        } else {
            false
        }
    }
}

// Get URI from file.
fun getUriFromFile(file: File, context: Context): Uri? =
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Uri.fromFile(file)
    } else {
        try {
            FileProvider.getUriForFile(context, context.packageName + ".provider", file)
        } catch (e: Exception) {
            if (e.message?.contains("ProviderInfo.loadXmlMetaData") == true) {
                throw Error("FileProvider doesn't exist or has no permissions")
            } else {
                throw e
            }
        }
    }

In API 29 emulator (without PDF applications) I found that it opened PDF in built-in library.


Ok guys, problem solved!

This is the code that i use to open a PDF file, stored on external memory:

File pdfFile = new File(Environment.getExternalStorageDirectory(),"namePdfFile.pdf");//File path
            if (pdfFile.exists()) //Checking if the file exists or not
            {
                Uri path = Uri.fromFile(pdfFile);
                Intent objIntent = new Intent(Intent.ACTION_VIEW);
                objIntent.setDataAndType(path, "application/pdf");
                objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(objIntent);//Starting the pdf viewer
            } else {

                Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();

            }