I've got a Webview Android app with a download button that tries to download an image from an S3 bucket. Like a half year, ago everything worked fine but suddenly it stopped working. If i go to the browser variant of the website everything still works. So i think it has something to do with the app.

I have a addDownloadListener and I added in the android manifest.

Below is my 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();

        }
    });
}

When i click the link this is the output in the log cat:

E: [] mConsumerName == NULL!!!!!! 2019-05-10 11:00:53.606 ? E: onTransact in code is: 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 Answers

0
Frank Alkema On

For some reason i don't know the addDownloadListener is never activated so that is why it never downloaded a image. What so i made a work around. In the MainActivity I had this function: visitProposedToLocationWithAction what checks if the new request the app makes stays within our webview. So I made a if else statement in that function with the following lines:

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);
    }
}

So now when I make a request to where the images are saved it downloads them.