【问题标题】:Use DownloadManager class to download file from WebView使用 DownloadManager 类从 WebView 下载文件
【发布时间】:2012-08-22 17:30:23
【问题描述】:

当单击 webview 上的链接时,我在这里有这个小代码,该链接是文件的链接,在本例中为 .mp4。此代码将转到默认 Web 浏览器并请求可以查看此文件类型的应用程序。

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

我想要的是当我单击该文件链接时,它会创建一个对话框,询问天气以下载或查看文件。如果单击下载,我想使用 DownloadManager 类来处理它并在后台下载该文件并在完成时发出警报。如果单击查看,我想创建意图,要求应用程序可以查看此文件而无需访问网络浏览器。

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

现在我得到了提示用户下载或播放用户单击的 mp4 文件的代码。但是当我点击播放或下载时,它一直有效,直到我第二次点击链接,如果有人可以纠正这个问题,上面的代码有什么问题。谢谢。

我对 Android 开发和 java 非常陌生,如果有人能指导我完成这一切,它将帮助我更快地学习。也对不起我的英语......

【问题讨论】:

    标签: java android android-intent webview


    【解决方案1】:

    您的问题的解决方案依赖于拦截您的 webview 尝试加载的 URL。创建一个WebViewClient 并覆盖 shouldOverrideUrlLoading 方法:

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

    然后,根据用户的选择,启动AsyncTask for downloading 来处理文件下载,或启动查看意图。

    【讨论】:

    • if (url.endsWith(".mp4")) { AlertDialog.Builder builder = new AlertDialog.Builder(?); builder.setMessage(R.string.dialog_title) .setCancelable(false) .setPositiveButton(R.string.dialog_download, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int ?) { // how i put async to download? } }) .setNegativeButton(R.string.dialog_play, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int ?) { // how to create intent to view mp4? } }); AlertDialog alert = builder.create(); return false; }
    • 按照我提供的链接使用异步任务进行下载。可以像这样创建查看 mp4 的意图:android-er.blogspot.ca/2011/05/…
    • 我只是用代码更新问题以提示用户下载和播放文件但是有一些问题,当我点击链接时它不起作用,直到我第二次点击链接,请帮助纠正那个代码。
    猜你喜欢
    • 2015-09-30
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多