如何从webview Android下载文件?

前端之家收集整理的这篇文章主要介绍了如何从webview Android下载文件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的代码下载加载网址页面,搜索歌曲后,我点击下载链接崩溃.没有太多关于如何让下载管理器使用Webview的教程.我究竟做错了什么?
  1. import java.io.File;
  2. import android.app.Activity;
  3. import android.app.DownloadManager;
  4. import android.content.Context;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.os.Environment;
  8. import android.webkit.WebView;
  9. import android.webkit.WebViewClient;
  10.  
  11.  
  12. public class List1 extends Activity {
  13.  
  14. WebView ourBrow;
  15.  
  16.  
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20.  
  21. // Use a custom layout file
  22. setContentView(R.layout.list1);
  23.  
  24. final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
  25.  
  26. final File destinationDir = new File (Environment.getExternalStorageDirectory(),getPackageName());
  27. if (!destinationDir.exists()) {
  28. destinationDir.mkdir(); // Don't forget to make the directory if it's not there
  29. }
  30.  
  31. ourBrow = (WebView) findViewById(R.id.wvBrowser);
  32. ourBrow.getSettings().setJavaScriptEnabled(true);
  33. ourBrow.setInitialScale(50);
  34. ourBrow.getSettings().setUseWideViewPort(true);
  35. ourBrow.setVerticalScrollBarEnabled(false);
  36. ourBrow.setHorizontalScrollBarEnabled(false);
  37. ourBrow.loadUrl("http://www.degjo.com");
  38.  
  39.  
  40.  
  41. ourBrow.setWebViewClient(new WebViewClient() {
  42. @Override
  43. public boolean shouldOverrideUrlLoading (WebView view,String url) {
  44. boolean shouldOverride = false;
  45. // We only want to handle requests for mp3 files,everything else the webview
  46. // can handle normally
  47. if (url.endsWith(".mp3")) {
  48. shouldOverride = true;
  49. Uri source = Uri.parse(url);
  50.  
  51. // Make a new request pointing to the mp3 url
  52. DownloadManager.Request request = new DownloadManager.Request(source);
  53. // Use the same file name for the destination
  54. File destinationFile = new File (destinationDir,source.getLastPathSegment());
  55. request.setDestinationUri(Uri.fromFile(destinationFile));
  56. // Add it to the manager
  57. manager.enqueue(request);
  58. }
  59. return shouldOverride;
  60. }
  61. });
  62.  
  63.  
  64.  
  65.  
  66.  
  67. }
  68. }

logcat的

  1. 02-18 19:45:44.891: E/AndroidRuntime(357): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:408)
  2. 02-18 19:45:44.891: E/AndroidRuntime(357): at android.content.ContentResolver.insert(ContentResolver.java:604)
  3. 02-18 19:45:44.891: E/AndroidRuntime(357): at android.app.DownloadManager.enqueue(DownloadManager.java:750)
  4. 02-18 19:45:44.891: E/AndroidRuntime(357): at com.example.androidbuttonsactivities.List1$1.shouldOverrideUrlLoading(List1.java:78)
  5. 02-18 19:45:44.891: E/AndroidRuntime(357): at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:216)
  6. 02-18 19:45:44.891: E/AndroidRuntime(357): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:323)
  7. 02-18 19:45:44.891: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
  8. 02-18 19:45:44.891: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
  9. 02-18 19:45:44.891: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:3683)
  10. 02-18 19:45:44.891: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
  11. 02-18 19:45:44.891: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:507)
  12. 02-18 19:45:44.891: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  13. 02-18 19:45:44.891: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  14. 02-18 19:45:44.891: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
  15. 02-18 19:45:48.401: I/Process(357): Sending signal. PID: 357 SIG: 9

解决方法

这可能取决于Android版本的版本问题,下面的代码将会在2.3版本上成功运行,检查一下,
  1. ourBrow.setWebViewClient(new WebViewClient() {
  2. @Override
  3. public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) {
  4. Log.d("WEB_VIEW_TEST","error code:" + errorCode + " - " + description);
  5. }
  6.  
  7. @Override
  8. public boolean shouldOverrideUrlLoading(WebView view,String url) {
  9. // handle different requests for different type of files
  10. // this example handles downloads requests for .apk and .mp3 files
  11. // everything else the webview can handle normally
  12. if (url.endsWith(".apk")) {
  13. Uri source = Uri.parse(url);
  14. // Make a new request pointing to the .apk url
  15. DownloadManager.Request request = new DownloadManager.Request(source);
  16. // appears the same in Notification bar while downloading
  17. request.setDescription("Description for the DownloadManager Bar");
  18. request.setTitle("YourApp.apk");
  19. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  20. request.allowScanningByMediaScanner();
  21. request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  22. }
  23. // save the file in the "Downloads" folder of SDCARD
  24. request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"SmartPigs.apk");
  25. // get download service and enqueue file
  26. DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
  27. manager.enqueue(request);
  28. }
  29. else if(url.endsWith(".mp3")) {
  30. // if the link points to an .mp3 resource do something else
  31. }
  32. // if there is a link to anything else than .apk or .mp3 load the URL in the webview
  33. else view.loadUrl(url);
  34. return true;
  35. }
  36. });

猜你在找的Android相关文章