java.lang.SecurityException:Destination必须位于外部存储上

时间:2013-04-20 03:26:09

标签: android

在android TV中,U盘mount / mnt / usb / sdb4,使用android DownloadManage,

 File pathyr = new File("/mnt/usb/sdb4");
            Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
            DownloadManager.Request request = new DownloadManager.Request(downloadUri);
            request.setDestinationUri(Uri.fromFile(new File( pathyr,"100mb.jpg") ));

04-20 10:54:05.593: W/dalvikvm(2315): threadid=1: thread exiting with uncaught exception (group=0x40f771f8)
04-20 10:54:05.665: E/AndroidRuntime(2315): FATAL EXCEPTION: main
04-20 10:54:05.665: E/AndroidRuntime(2315): java.lang.SecurityException: Destination must be on external storage: file:///mnt/usb/sdb4/100mb.test.test
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.os.Parcel.readException(Parcel.java:1327)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.content.ContentProviderProxy.insert(ContentProviderNative.java:415)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.content.ContentResolver.insert(ContentResolver.java:730)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at com.exercise.download.DownloadManager.enqueue(DownloadManager.java:750)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at com.exercise.AndroidDownloadManager.AndroidDownloadManagerActivity$1.onClick(AndroidDownloadManagerActivity.java:71)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.View.performClick(View.java:3511)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.View.onKeyUp(View.java:6073)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.widget.TextView.onKeyUp(TextView.java:5595)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.KeyEvent.dispatch(KeyEvent.java:3010)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.View.dispatchKeyEvent(View.java:5500)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1879)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.app.Activity.dispatchKeyEvent(Activity.java:2325)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3300)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.os.Looper.loop(Looper.java:137)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at java.lang.reflect.Method.invoke(Method.java:511)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:579)
04-20 10:54:05.665: E/AndroidRuntime(2315):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:4)

目前,DownloadManager请求仅接受“标准”外部存储上的路径,由Environment.getExternalStoragePublicDirectory(yourDirOfChoice)Environment.getExternalStorageDirectory()检索;否则,它会抛出此异常。

有关详细信息,请参阅this StackOverflow问题。

答案 1 :(得分:0)

这不适用于Android Pie。 你需要打电话

.setDestinationInExternalFilesDir()
//Pasing in the getFilesDir().absolutePath, your file