将图像文件下载到WebView Android

时间:2013-08-13 20:36:19

标签: android webview android-webview

我有几种文件格式的弧形,如JPG,PDF,ZIP,我想将文件下载到我的应用程序中。 PDF和ZIP文件正在下载,但我无法下载JPG文件。知道如何下载JPG文件?

HTML页面http://www.sotkora.com/xy/ex_download.html

  public class DownloadingExample extends Activity {

    WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_downloading_example);


        this.webView=(WebView) this.findViewById(R.id.webView_IF);
        this.webView.getSettings().setSupportZoom(false);
        this.webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        this.webView.loadUrl("http://sotkora.com/xy/ex_download.html");
        this.webView.setWebViewClient(new WebViewClientDemo());
        this.webView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                            String contentDisposition, String mimetype,
                            long contentLength) {

                                          Uri uri = Uri.parse(url);
           Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                    startActivity(intent);
            }
    });

    }
  private class WebViewClientDemo extends WebViewClient {
    @Override

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
     view.loadUrl(url);
      return true;
     }

  }

HTML code:

<div><a href="http://www.sotkora.com/xy/images/rose.jpg" download="JPGPhoto">Download JPG Photo</a></div><br/>
        <div><a href="http://www.sotkora.com/xy/files/load_test.pdf" download="pdf_test">Download PDF File</a></div><br/>
        <div><a href="http://www.sotkora.com/xy/files/load_test.zip" download="ZIP_test">Download ZIP File</a></div><br/>

1 个答案:

答案 0 :(得分:0)

如果您使用正确的标题下载,您应该可以执行以下操作... (您需要实现自己的下载过程)

mWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
            String contentDisposition, String mimetype,
            long contentLength) {
        // Download via android code
    }
});