Comment télécharger des fichiers à partir de webview Android?

Mon code ci-dessous charges de l'url de la page d'amende et après la recherche d'un morceau lorsque je clique sur le lien de téléchargement, il se bloque. Il n'y a pas beaucoup de tutoriels sur la façon d'obtenir le manager de téléchargement de travailler avec une webview. Ce que je fais mal?

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

source d'informationauteur Dritan Berna | 2014-02-18