gpt4 book ai didi

android - WebView + Blob URL + 下载图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:06 32 4
gpt4 key购买 nike

好久没用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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com