Open PDF file using intent programmatically in android

Viewed 1360

I have an android app wherein I need to open my pdf file from url. For that, I have used following code to invoke supporting applications to open my pdf file but despite having more than 5 such applications, just two apps e.g. Adobe Reader and Goodgle Drive are listed.

intent.setDataAndType(Uri.parse(url), "application/pdf");
startActivity(intent);```

But, I'm in need to open pdf using other compatible apps already installed. Please help resolve the ibid issue.
Thanks 
2 Answers

use this

private fun ViewFile(pdfFile: File) {
 Log.e("!!pdfFile",pdfFile.absolutePath)

 val path: Uri
 val pdfIntent = Intent(Intent.ACTION_VIEW)
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 try {
 val m: Method = StrictMode::class.java.getMethod("disableDeathOnFileUriExposure")
 m.invoke(null)
 } catch (e: Exception) {
 e.printStackTrace()
 }
 path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID.toString() + ".provider", pdfFile)

 // path = Uri.parse(pdfFile.getAbsolutePath());

 // pdfIntent.setDataAndType(path, "application/pdf");
 pdfIntent.setDataAndType(path, "application/pdf")
 pdfIntent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY
 pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
 } else {
 path = Uri.fromFile(pdfFile)
 pdfIntent.setDataAndType(path, "application/pdf")
 }


 /* Uri path = FileProvider.getUriForFile(getActivity(),
 BuildConfig.APPLICATION_ID + ".provider",
 pdfFile);*/try {
 startActivity(pdfIntent)
 } catch (e: ActivityNotFoundException) {
 Toast.makeText(this, getString(R.string.lblnoapplicationfound), Toast.LENGTH_SHORT).show()
 }
}

try this simple way to open pdf from url

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
 startActivity(browserIntent);
Related