如何在Android开放中的WebView本地PDF文件?文件、如何在、WebView、Android

2023-09-04 09:41:15 作者:街角~祝福你

我想在web视图中打开本地(SD卡)的PDF文件。

i want to open local(sdcard) pdf file in webview.

我已经尝试这一点,也有很多尝试:

i already try this and also many try:

webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setPluginsEnabled(true);
        webview.getSettings().setAllowFileAccess(true);
        File file = new File(Environment.getExternalStorageDirectory()
                + "/test.pdf");

        final Uri uri = Uri.fromFile(file);

        webview.loadUrl(uri.toString());

但仍然没有打开它,所以让我知道我可以在web视图中打开PDF文件?

but still not open it so let me know how i can open pdf in webview?

问候, 吉里什

推荐答案

您不能。

try
{
 Intent intentUrl = new Intent(Intent.ACTION_VIEW);
 intentUrl.setDataAndType(uri, "application/pdf");
 intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 mActivity.startActivity(intentUrl);
}
catch (ActivityNotFoundException e)
{
 Toast.makeText(mActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}