Android Web View 无法从亚马逊服务网址查看 PDF

标签 android pdf amazon-web-services webview

我正在尝试通过调用亚马逊 url 在 android webview 中显示 pdf 文件。但它只显示白屏。没有加载。

当我使用亚马逊以外的网址时,它会在 WebView 中显示 pdf 文件。 我也试过这个: <强> http://docs.google.com/gview?embedded=true&url= "+ 我的网址

我也尝试过写 url:而且效果很好。 http://www.durgasoft.com/Android%20Interview%20Questions.pdf

如果有人有任何建议,请指导我。

这是我的代码供您引用:

 webView.getSettings().setJavaScriptEnabled(true);
 webView.getSettings().setPluginState(PluginState.ON);
 String url = Common.getPdfFromAmazon("52f3761d290c4.pdf");   
 webView.loadUrl(url);

Android Menifest.xml also give Internet Permission:
**<uses-permission android:name="android.permission.INTERNET" />**

i can also try this "http://docs.google.com/gview?embedded=true&url=" + url ;

Here is my logcat image.

谢谢。

最佳答案

要显示来自亚马逊网络服务的 PDF,您需要先下载 PDF 并将其存储到您的设备,然后通过您设备上可用的 PDF 阅读器/查看器应用程序打开它。

1>> 调用 DownloadFileAsync() 调用下载过程并传递您的亚马逊网络服务 url。

new DownloadFileAsync().execute(url);

2>> 在 AsyncTask 中执行下载 PDF 过程。

class DownloadFileAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(final String... aurl) {

        try {
            String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            File dir = new File(extStorageDirectory, "pdf");
            if(dir.exists()==false) {
                dir.mkdirs();
            }
            File directory = new File(dir, "original.pdf");
            try {
                if(!directory.exists())
                    directory.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            int lenghtOfFile = conexion.getContentLength();
            conexion.connect();
            conexion.setReadTimeout(10000);
            conexion.setConnectTimeout(15000); // millis

            FileOutputStream f = new FileOutputStream(directory);

            InputStream in = conexion.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.flush();
            f.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    protected void onPostExecute(String unused) {
    }
}

3>> 下载pdf后调用showPdfFromSdCard()

public static  void showPdfFromSdCard(Context ctx) {
    File file = new File(Environment.getExternalStorageDirectory() + "/pdf/original.pdf");
    PackageManager packageManager = ctx.getPackageManager();
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    testIntent.setType("application/pdf");
    List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        ctx.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(ctx,
                "No Application Available to View PDF",
                Toast.LENGTH_SHORT).show();
    }
}

4>> 在你的onResume()

调用deletePdfFromSdcard()
public static void deletePdfFromSdcard(){
    File file = new    File(Environment.getExternalStorageDirectory()+"/pdf/original.pdf");
    boolean pdfDelete = file.delete();
}

关于Android Web View 无法从亚马逊服务网址查看 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21604147/

相关文章:

android - 如何在特定的LinearLayout中添加Canvas?

html - UIMarkupTextPrintFormatter 从不渲染 base64 图像

html - 是@page :last really something?

amazon-web-services - 如何在各种已部署应用程序的 kubernetes 上下文之间切换?

java - 在aws实例中设置反向代理,运行tomcat服务器

android - 有没有办法在 Android 中定义 EditText 的最小值和最大值?

android - 如何知道文本在 Jetpack Compose 上是否可见?

android - 仅允许在 webview 上打开网站

java - 使用 iText 将带有图像的 HTML 转换为 PDF

python - 如何使用 boto3 将 S3 对象保存到文件中