简体   繁体   中英

Open PDF file using intent programmatically in android

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 eg 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 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM