WebView + Blob URL +下载图像

时间:2017-07-18 11:27:18

标签: android image webview download bloburls

经过很长一段时间,我使用了WebView。那么让我问一下我的阶段问题。

- 我有Blob URL的谷歌,但我找到了解决方案或任何提示,所以我必须在这里发帖题。

- 我在Android端有WebView来加载一个网站。在他们的按钮上"SAVE-AS" to download an image

- 当我在Google Chrome app上尝试时,working fine并正确下载。

- 我的应用webviewDownloadListener相同的情况 我得到了类似

的Blob URL
  

团块:http://-----cantshare-----.com/7e7452c8-62ca-4231-8640-0e9de715e073

所以downloadmanger download nothing

检查我的代码

webview = (WebView) findViewById(R.id.webview);


WebSettings settings = webview.getSettings();
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

final ProgressDialog progressBar = ProgressDialog.show(this, "WebView Example", "Loading...");

webview.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i(TAG, "Processing webview url click...");
        view.loadUrl(url);
        return true;
    }

    public void onPageFinished(WebView view, String url) {
        Log.i(TAG, "Finished loading URL: " + url);
        if (progressBar.isShowing()) {
            progressBar.dismiss();
        }
    }

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.e(TAG, "Error: " + description);

    }
});

webview.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {

        Uri webpage = Uri.parse(url);

        if (!url.startsWith("http://") && !url.startsWith("https://")) {
            webpage = Uri.parse("http://" + url);
        }

        DownloadManager.Request request = new DownloadManager.Request(
                webpage);


        request.setMimeType(mimetype);


        String cookies = CookieManager.getInstance().getCookie(url);


        request.addRequestHeader("cookie", cookies);


        request.addRequestHeader("User-Agent", userAgent);


        request.setDescription("Downloading file...");


        request.setTitle(URLUtil.guessFileName(url, contentDisposition,
                mimetype));


        request.allowScanningByMediaScanner();


        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalFilesDir(Main2Activity.this,
                Environment.DIRECTORY_DOWNLOADS,".pdf");
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
        Toast.makeText(getApplicationContext(), "Downloading File",
                Toast.LENGTH_LONG).show();
    }
});

Q1 - 任何人都可以提供建议如何从Blob URL下载文件?
Q2 - 必须更改服务器端?
Q3 - 以其他方式代替Downloadermanager?

修改

我尝试了.pdf /.png/.jpegno luck;(

setDestinationInExternalFilesDir(Main2Activity.this,
                Environment.DIRECTORY_DOWNLOADS,".pdf");

1 个答案:

答案 0 :(得分:1)

我在同一个噩梦中。我所做的是转换Base64字符串中的Blob URL数据,并使用此字符串创建了一个" .pdf"文件。看看我的answer