gpt4 book ai didi

java - Android Webview 应用程序无法下载图像

转载 作者:行者123 更新时间:2023-12-02 05:49:34 28 4
gpt4 key购买 nike

我有一个带有下载按钮的 Webview Android 应用程序,该应用程序尝试从 S3 存储桶下载图像。大约半年前,一切正常,但突然就停止工作了。如果我转到网站的浏览器变体,一切仍然有效。所以我认为这与应用程序有关。

我有一个 addDownloadListener 并添加到 Android list 中。

下面是我的 addDownloadListener:

私有(private)无效addDownloadListener() { TurbolinksSession.getDefault(此) . Activity (这个) .适配器(这个) .view(turbolinksView) .getWebView() .setDownloadListener(new DownloadListener() {

        @Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimeType,
long contentLength) {

String filename = URLUtil.guessFileName(url, contentDisposition, mimeType);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Download is gestart", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();

}
});
}

当我单击链接时,这是日志猫中的输出:

E: [] mConsumerName == NULL!!!!!!2019-05-10 11:00:53.606 ? E:onTransact中代码为:103

2019-05-10 10:45:51.840 ? E: win=Window{104d4cf u0 com.app.name.here.MainActivity} destroySurfaces: appStopped=true win.mWindowRemovalAllowed=false win.mRemoveOnExit=false

最佳答案

出于某种原因,我不知道 addDownloadListener 从未被激活,所以这就是它从未下载图像的原因。所以我做了一个解决办法。在 MainActivity 中,我有这个函数:visitProposeToLocationWithAction,它检查应用程序发出的新请求是否保留在我们的 web View 中。因此,我在该函数中使用以下几行创建了 if else 语句:

public void visitProposedToLocationWithAction(String location, String action) {
if(location.contains(AMAZON_URL)) {

String url = location;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));



request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Downloads");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Download is gestart", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();

} else {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(INTENT_URL, location);
this.startActivity(intent);
}
}

所以现在当我向保存图像的位置发出请求时,它会下载它们。

关于java - Android Webview 应用程序无法下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56062665/

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