13

很久以后,我用的是WebView。所以让我问一下我有什么相位问题。

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

-我WebView在android端加载一个网站。在他们的 On Button 处"SAVE-AS" to download an image

- 现在,当我尝试它 Google Chrome appworking fine正确下载时。

- 现在,webview在我的应用程序中,DownloadListener 我得到了类似的 Blob URL

斑点: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();
    }
});

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

编辑

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

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

1 回答 1

3

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

于 2018-02-23T19:50:11.180 回答