WebView + Blob URL +下载图像

Vis*_*tel 8 android image download webview bloburls

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

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

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

- 当我尝试它 Google Chrome app是的它working fine并正确下载.

- webview在我的应用程序中DownloadListener 我使用Blob URL的情况相同

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

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

Q1 -任何人都给出建议如何从Blob URL下载文件?
Q2 - 必须更改服务器端?
Q3 - 除了Downloadermanager之外的任何其他方式?

编辑

我试过 .pdf /.png/.jpeg,但no luck (

setDestinationInExternalFilesDir(Main2Activity.this,
                Environment.DIRECTORY_DOWNLOADS,".pdf");
Run Code Online (Sandbox Code Playgroud)

Kev*_*rez 5

我也陷入了同样的噩梦。我所做的是将 Blob URL 数据转换为 Base64 字符串,并使用该字符串创建了一个“.pdf”文件。看看这个SO答案

  • 嗨,凯文...请在代码片段*..我的答案的外观..*中更新您的答案,以匹配*...我的SO答案的外观..*,以避免人们要求您发布代码..并添加末尾的点;p (2认同)