Cannot open PDF file in external app

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();

            }

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


Setting data as "file:///" + filePath worked out for me.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(filePath));
intent.setDataAndType(Uri.parse("file:///" + filePath), "application/pdf");
context.startActivity(intent);