使用下载管理器类,从的WebView下载文件 [英] Use DownloadManager class to download file from WebView

查看:182
本文介绍了使用下载管理器类,从的WebView下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这个小code这里的时候,点击的WebView链接是一个链接到一个文件,在这种情况下.MP4。这code将进入默认的Web浏览器,并请求应用程序,可以查看此文件类型。

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

我要的是当我点击该文件链接,它创建要求气象下载或查看文件的对话框。如果点击下载,我想使用下载管理器类来处理它,并下载该文件的背景和警报完成时。如果单击视图,我想创建意图索要应用程序,可以不用去的Web浏览器查看此文件。

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

现在我得到这个code,提示用户下载或播放,用户点击的MP4文件。但是当我点击播放或下载这是工作,直到我点击链接的第二次,有什么不对的code以上,如果任何人都可以解决此请。谢谢。

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.

我很新至Android开发和Java中,如果任何人都可以指导我通过这个它会帮助我学得更快。而且也对不​​起我的英语水平...

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...

推荐答案

该问题的解决方案依赖于拦截你的We​​bView试图加载的URL。创建 WebViewClient 并覆盖shouldOverrideUrlLoading方式:

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

然后,根据用户选择什么,启动一个<一href="http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog">AsyncTask下载处理下载文件,或启动的意图进行查看。

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

这篇关于使用下载管理器类,从的WebView下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆