Webview应用程序不下载Android图像

时间:2019-05-09 15:18:50

标签: java android image webview downloading

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

我有一个addDownloadListener,并且已添加到android清单中。

下面是我的addDownloadListener:

private void addDownloadListener(){         TurbolinksSession.getDefault(this)                 .activity(this)                 .adapter(this)                 .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

1 个答案:

答案 0 :(得分:0)

由于某种原因,我不知道addDownloadListener从未被激活过,所以这就是为什么它从未下载过映像。所以我做了个变通。在MainActivity中,我具有以下功能:visitProposedToLocationWithAction什么会检查应用程序发出的新请求是否停留在我们的Web视图中。因此,我在该函数中用以下几行做出了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);
    }
}

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