我正在尝试以这种方式使用Android DownloadManager类: super.onCreate(savedInstanceState); setContentView(R.layout.main);
Request r = new Request(Uri.parse("http://url"));
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.ext"); // also tried all other possible ways to set up destination
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(r);
在最后一行,dm.enqueue(r)
应用程序崩溃。日志猫错误:
05-12 13:53:55.408: E/JavaBinder(6814): *** Uncaught remote exception! (Exceptions are not yet supported across processes.)
05-12 13:53:55.408: E/JavaBinder(6814): java.lang.NoSuchMethodError: getExternalStorages
05-12 13:53:55.408: E/JavaBinder(6814): at com.android.providers.downloads.Helpers.isFilenameValid(Helpers.java:652)
05-12 13:53:55.408: E/JavaBinder(6814): at com.android.providers.downloads.DownloadProvider.checkFileUriDestination(DownloadProvider.java:613)
05-12 13:53:55.408: E/JavaBinder(6814): at com.android.providers.downloads.DownloadProvider.insert(DownloadProvider.java:500)
05-12 13:53:55.408: E/JavaBinder(6814): at android.content.ContentProvider$Transport.insert(ContentProvider.java:198)
05-12 13:53:55.408: E/JavaBinder(6814): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:146)
05-12 13:53:55.408: E/JavaBinder(6814): at android.os.Binder.execTransact(Binder.java:324)
05-12 13:53:55.408: E/JavaBinder(6814): at dalvik.system.NativeStart.run(Native Method)
05-12 13:53:55.408: W/dalvikvm(6814): threadid=7: thread exiting with uncaught exception (group=0x2aac8620)
05-12 13:53:55.408: E/AndroidRuntime(6814): FATAL EXCEPTION: Binder Thread #1
05-12 13:53:55.408: E/AndroidRuntime(6814): java.lang.NoSuchMethodError: getExternalStorages
05-12 13:53:55.408: E/AndroidRuntime(6814): at com.android.providers.downloads.Helpers.isFilenameValid(Helpers.java:652)
05-12 13:53:55.408: E/AndroidRuntime(6814): at com.android.providers.downloads.DownloadProvider.checkFileUriDestination(DownloadProvider.java:613)
05-12 13:53:55.408: E/AndroidRuntime(6814): at com.android.providers.downloads.DownloadProvider.insert(DownloadProvider.java:500)
05-12 13:53:55.408: E/AndroidRuntime(6814): at android.content.ContentProvider$Transport.insert(ContentProvider.java:198)
05-12 13:53:55.408: E/AndroidRuntime(6814): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:146)
05-12 13:53:55.408: E/AndroidRuntime(6814): at android.os.Binder.execTransact(Binder.java:324)
05-12 13:53:55.408: E/AndroidRuntime(6814): at dalvik.system.NativeStart.run(Native Method)
但是,如果我不以任何方式指定目的地,则文件已成功下载。
我完全不明白问题出在哪里。你能帮我吗?
在模拟器(或其他一些未修改的环境中)上尝试您的代码。您也可以考虑尝试使用已知的工作代码。