android - WebView + Blob URL + 下载图片

标签 android image webview download bloburls

好久没用WebView了。所以让我问一下我有什么相位问题。

-我在 Google 上搜索了 Blob URL,但我找不到解决方案或任何提示,所以我必须在此处发布问题。

-我在 android 端有 WebView 来加载网站。在他们的 On 按钮上 “SAVE-AS”下载图像

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

-现在在我的应用程序 DownloadListener 中使用 webview 的情况相同 我得到了 Blob URL 之类的东西

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

所以 downloadmanger 什么都不下载

检查我的代码

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");

最佳答案

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

关于android - WebView + Blob URL + 下载图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45165499/

相关文章:

java - 自定义标题栏中的问题(android)

html - 如何将与图像位于同一行的表格居中?

javascript - 页面重新加载 (Webview.loadUrl) 导致 Javascript 未被(完全)处理

javascript - android webview = 找不到网页 = 在哪里添加 onReceivedError

javascript - 在不重新加载页面的情况下更改 Android Webview 哈希

android - GLES20 纹理在某些设备上不起作用

java - 在 JDK 7u2 上重写接口(interface)方法时出现 @override 编译器错误

android - WARN/AudioFlinger(33) : write blocked for 76 msecs, 7773 延迟写入,线程 0xb3f0

javascript - 类似于 google+ 相册 View 的动态图像排序和裁剪

javascript - 带有缩放元素的响应式网格