【问题标题】:Webview app doesn't download images AndroidWebview 应用程序不下载图像 Android
【发布时间】:2019-05-09 15:18:50
【问题描述】:

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

我有一个 addDownloadListener 并添加到了 android 清单中。

下面是我的 addDownloadListener:

私人无效 addDownloadListener() { TurbolinksSession.getDefault(this) .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

【问题讨论】:

  • 听起来授权已过期。我没有看到您在此处向 s3 发送身份验证令牌;检查文件或容器本身的权限。
  • 当然,尝试通过 CURL(或邮递员)下载图像 - 或者将 Fiddler 设置为代理,看看是否还有其他事情发生。
  • @Micromuncher 在这种情况下我该怎么做?
  • 这怎么可能是问题所在?在浏览器上它仍然可以正常工作。

标签: java android image webview download


【解决方案1】:

由于某种原因,我不知道 addDownloadListener 从未激活,这就是它从未下载图像的原因。所以我做了一个工作。在 MainActivity 我有这个功能: visitProposedToLocationWithAction 检查应用程序发出的新请求是否保留在我们的 webview 中。所以我在该函数中做了一个 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);
    }
}

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多