Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何从webview Android下载文件?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的代码下载加载网址页面,搜索歌曲后,我点击下载链接崩溃.没有太多关于如何让下载管理器使用Webview的教程.我究竟做错了什么?
import java.io.File;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.bundle;
import android.os.Environment;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class List1 extends Activity {

        WebView ourBrow;


    @Override
    public void onCreate(Bundle savedInstanceStatE) {
        super.onCreate(savedInstanceStatE);

        // Use a custom layout file
        setContentView(R.layout.list1);

        final DownloadManager manager = (DownloadManager) getSystemservice(Context.DOWNLOAD_serviCE);

     final File desTinationDir = new File (Environment.getExternalStorageDirectory(),getPackagename());
     if (!desTinationDir.exists()) {
         desTinationDir.mkdir(); // Don't forget to make the directory if it's not there
     }

        ourBrow = (WebView) findViewById(R.id.wvBrowser);
        ourBrow.getSetTings().setJavaScriptEnabled(true);
        ourBrow.seTinitialScale(50); 
        ourBrow.getSetTings().setUseWideViewPort(true); 
        ourBrow.setVerticalScrollBarEnabled(false);
        ourBrow.setHorizontalScrollBarEnabled(false);
        ourBrow.loadUrl("http://www.degjo.com");



        ourBrow.setWebViewClient(new WebViewClient() {
            @Override
            public Boolean shouldOverrideUrlLoading (WebView view,String url) {
                Boolean shouldOverride = false;
                // We only want to handle requests for mp3 files,everything else the webview
                // can handle normally
                if (url.endsWith(".mp3")) {
                    shouldOverride = true;
                    Uri source = Uri.parse(url);

                    // Make a new request poinTing to the mp3 url
                    DownloadManager.request request = new DownloadManager.request(sourcE);
                    // Use the same file name for the desTination
                    File desTinationFile = new File (desTinationDir,source.getLastPathSegment());
                    request.setDesTinationUri(Uri.fromFile(desTinationFilE));
                    // Add it to the manager
                    manager.enqueue(request);
                }
                return shouldOverride;
            }
        });





    }
}

logcat的

02-18 19:45:44.891: E/AndroidRuntime(357):  at android.content.ContentProviderProxy.insert(ContentProviderNative.java:408)
02-18 19:45:44.891: E/AndroidRuntime(357):  at android.content.ContentResolver.insert(ContentResolver.java:604)
02-18 19:45:44.891: E/AndroidRuntime(357):  at android.app.DownloadManager.enqueue(DownloadManager.java:750)
02-18 19:45:44.891: E/AndroidRuntime(357):  at com.example.androidbuttonsactivities.List1$1.shouldOverrideUrlLoading(List1.java:78)
02-18 19:45:44.891: E/AndroidRuntime(357):  at android.webkit.CallBACkProxy.uiOverrideUrlLoading(CallBACkProxy.java:216)
02-18 19:45:44.891: E/AndroidRuntime(357):  at android.webkit.CallBACkProxy.handlemessage(CallBACkProxy.java:323)
02-18 19:45:44.891: E/AndroidRuntime(357):  at android.os.Handler.dispatchmessage(Handler.java:99)
02-18 19:45:44.891: E/AndroidRuntime(357):  at android.os.Looper.loop(Looper.java:123)
02-18 19:45:44.891: E/AndroidRuntime(357):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 19:45:44.891: E/AndroidRuntime(357):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 19:45:44.891: E/AndroidRuntime(357):  at java.lang.reflect.Method.invoke(Method.java:507)
02-18 19:45:44.891: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 19:45:44.891: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 19:45:44.891: E/AndroidRuntime(357):  at dalvik.system.NativeStart.main(Native Method)
02-18 19:45:48.401: I/Process(357): Sending signal. PID: 357 SIG: 9

解决方法

这可能取决于Android版本的版本问题,下面的代码将会在2.3版本上成功运行,检查一下,
ourBrow.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) {
                Log.d("WEB_VIEW_TEST","error code:" + errorCode + " - " + description);
        }

        @Override
        public Boolean shouldOverrideUrlLoading(WebView view,String url) {
                // handle different requests for different type of files
                // this example handles downloads requests for .apk and .mp3 files
                // everything else the webview can handle normally
                if (url.endsWith(".apk")) {
                    Uri source = Uri.parse(url);
                    // Make a new request poinTing to the .apk url
                    DownloadManager.request request = new DownloadManager.request(sourcE);
                    // appears the same in Notification bar while downloading
                    request.setDescription("Description for the DownloadManager Bar");
                    request.settitle("YourApp.apk");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODEs.HONEYCOMB) {
                        request.allowScAnningBymediaScAnner();
                        request.setNotificationVisibility(DownloadManager.request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    }
                    // save the file in the "Downloads" folder of SDCARD
                    request.setDesTinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"smartPigs.apk");
                    // get download service and enqueue file
                    DownloadManager manager = (DownloadManager) getSystemservice(Context.DOWNLOAD_serviCE);
                    manager.enqueue(request);
                }
                else if(url.endsWith(".mp3")) {
                    // if the link points to an .mp3 resource do something else
                }
                // if there is a link to anything else than .apk or .mp3 load the URL in the webview
                else view.loadUrl(url);
                return true;                
        }
    });

大佬总结

以上是大佬教程为你收集整理的如何从webview Android下载文件?全部内容,希望文章能够帮你解决如何从webview Android下载文件?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。