WebView + Blob URL + 下载图片

14

好久没用 WebView 了,所以我想问一下我有一个阶段性问题。

- 我在谷歌上搜索了 Blob URL,但没有找到解决方案或任何提示,所以我不得不在这里发布问题。

- 我在 Android 端有一个 WebView 来加载一个网站。在他们的按钮上点击 "SAVE-AS" 下载图片

- 现在当我在 Google Chrome 应用 上尝试时,它可以正常工作并且下载正确。

- 现在同样的情况出现在我的应用程序中的 WebView DownloadListener 中, 我得到了类似于Blob URL 的东西

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

所以下载管理器 什么也没下载

检查我的代码

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/.jpeg 但是没有成功 ;(

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

你下载文件了吗? - Rohit
读取 blob 有什么进展吗? - Rahul Khurana
1个回答

4

我曾经遇到过同样的问题。我的解决办法是将Blob URL数据转换为Base64字符串,并使用该字符串创建一个“.pdf”文件。请参考我的答案


2
嗨,Kevin...请更新你在代码片段*..look of my answer..中的答案,以匹配我的SO答案的...look of my SO answer..*,以避免人们要求你发布代码。并在末尾加上一个句点; p - ZF007

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接