[英]Open PDF file using intent programmatically in android
I have an android app wherein I need to open my pdf file from url.我有一个 android 应用程序,我需要从 url 打开我的 pdf 文件。 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.为此,我使用以下代码调用支持应用程序来打开我的 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
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试试这个简单的方法从 url 打开 pdf
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.