Use DownloadManager class to download file from We

2019-03-22 02:29发布

I have this little code here when click a link on webview that is a link to a file, in this case .mp4. this code will go to default web browser and request app that can view this file type.

myWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
        String contentDisposition, String mimetype,
        long contentLength) {
      Intent i = new Intent(Intent.ACTION_VIEW);
      i.setData(Uri.parse(url));
      startActivity(i);
    }
});

What i want is when i click that file link, it create a dialog that ask weather to download or view the file. If click download, i want to use DownloadManager class to handle it and download that file in the background and alert when completed. And if click view, i want to create intent that ask for app that can view this file without going to the web browser.

    private class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, final String url) {

        if (url.endsWith(".mp4")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage(R.string.dialog_title)
                .setCancelable(false)
                .setPositiveButton(R.string.dialog_download, new DialogInterface.OnClickListener() {                        

                    public void onClick(DialogInterface dialog, int id) {
                                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                                request.setDescription("Some descrition");
                                request.setTitle("Some title");
                                // in order for this if to run, you must use the android 3.2 to compile your app
                                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");

                                // get download service and enqueue file
                                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                                manager.enqueue(request);
                    }
                })
                .setNegativeButton(R.string.dialog_play, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        WebView myWebView = (WebView) findViewById(R.id.webview);                           
                        myWebView.setDownloadListener(new DownloadListener() {
                            public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_VIEW);
                                intent.setDataAndType(Uri.parse(url), "video/mp4");
                                startActivity(intent);
                            }
                        });

                    }
                });
            AlertDialog alert = builder.create();
            alert.show();
        }
        return false;
    }
}

Now i got this code that prompt user to download or play mp4 file that the user clicked. But when i click Play or Download it is work until i click the link the second time, what's wrong with the code above if anyone can correct this please. thanks.

I'm very new to Android developing and java, if anyone could guide me through this it will help me learn faster. And also sorry for my English...

1条回答
我命由我不由天
2楼-- · 2019-03-22 03:09

The solution to your problem relies on intercepting the URL that your webview is trying to load. Create a WebViewClient and overwrite the shouldOverrideUrlLoading method:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.endsWith(".mp3")) { //or whatever other extension
      //Prompt user for action (save or view)
    }
    return false;
 }

Then, depending on what the user chooses, start an AsyncTask for downloading to handle downloading the file, or launch the intent for viewing.

查看更多
登录 后发表回答