簡體   English   中英

在 android 中以編程方式使用意圖打開 PDF 文件

[英]Open PDF file using intent programmatically in android

我有一個 android 應用程序,我需要從 url 打開我的 pdf 文件。 為此,我使用以下代碼調用支持應用程序來打開我的 pdf 文件,但盡管有超過 5 個此類應用程序,但僅列出了兩個應用程序,例如 Adobe Reader 和 Goodgle Drive。

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 

用這個

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

試試這個簡單的方法從 url 打開 pdf

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM