Android Web View无法从Amazon Service Url查看PDF

Dha*_*ani 1 pdf android webview amazon-web-services

我想通过调用amazon url在android webview中显示pdf文件.但它只显示白屏.无法加载.

当我使用其他网址时,亚马逊在webview中显示pdf文件.我也尝试了这个:http: //docs.google.com/gview?entedded = true&url ="+ MYURL

我也尝试过写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 ;
Run Code Online (Sandbox Code Playgroud)

这是我的logcat图像.

谢谢.

小智 5

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

1 >>致电DownloadFileAsync()调用下载流程并传递您的亚马逊网络服务网址.

new DownloadFileAsync().execute(url);
Run Code Online (Sandbox Code Playgroud)

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) {
    }
}
Run Code Online (Sandbox Code Playgroud)

3 >> showPdfFromSdCard()下载pdf后致电.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

4 >>打电话给deletePdfFromSdcard()onResume()

public static void deletePdfFromSdcard(){
    File file = new    File(Environment.getExternalStorageDirectory()+"/pdf/original.pdf");
    boolean pdfDelete = file.delete();
}
Run Code Online (Sandbox Code Playgroud)