Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.SecurityException: Destination must be on external storage

Tags:

android

in android TV, U disk mount /mnt/usb/sdb4, use 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)
like image 662
Sunny.wan Avatar asked Dec 04 '25 16:12

Sunny.wan


2 Answers

At the moment, a DownloadManager request accepts only paths on the "standard" external storage, as retrieved by Environment.getExternalStoragePublicDirectory(yourDirOfChoice) or Environment.getExternalStorageDirectory(); otherwise, it throws this exception.

For more info, see this StackOverflow question.

like image 92
dentex Avatar answered Dec 06 '25 07:12

dentex


This will not work for Android Pie. you need to call

.setDestinationInExternalFilesDir()
//Pasing in the getFilesDir().absolutePath, your file
like image 20
1Developer Avatar answered Dec 06 '25 06:12

1Developer