1

I am trying to move files from any location (including internal device storage) to SD Card,

For that I Have

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in my manifest, and checked that it did work by checking if premissions are granted using :

Contex.checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") and Contex.checkCallingOrSelfPermission("android.permission.READ_EXTERNAL_STORAGE")

and I have tried 3 diffrent ways to do that :

  1. Files.move(original, newPath1), which comes with Guava com.google.common.io
  2. this method :

    public static void move(File src, File dst) throws IOException {
        FileInputStream inStream = new FileInputStream(src);
        FileOutputStream outStream = new FileOutputStream(dst);
        FileChannel inChannel = inStream.getChannel();
        FileChannel outChannel = outStream.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        inStream.close();
        outStream.close();
    }
    
  3. original.renameTo(newPath1);

I read alot of SO posts about this, and all solutions i tried did not work, I made sure the SD Card is mounted, and that i didnt have my device connected to PC while trying. I even tried 2 diffrent devices with SD Cards. Notice that i could access the image with BitmapFactory.decode() with the exact same path and i could load that image to imageview.

I am always getting EACCES (Permission denied). I dont know what else to do to solve this :(

11-13 11:11:54.358 19192-19372/xaday.ofek.ron.xaday W/System.err: java.io.FileNotFoundException: /storage/sdcard1/DCIM/thai2/IMG-20150702-WA0001.jpg: open failed: EACCES (Permission denied)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:465)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err:     at com.google.common.io.Files$FileByteSink.openStream(Files.java:245)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err:     at com.google.common.io.Files$FileByteSink.openStream(Files.java:233)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err:     at com.google.common.io.ByteSource.copyTo(ByteSource.java:248)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err:     at com.google.common.io.Files.copy(Files.java:458)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err:     at com.google.common.io.Files.move(Files.java:673)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err:     at xaday.ofek.ron.xaday.XaDayIntentService.handleActionMove(XaDayIntentService.java:128)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err:     at xaday.ofek.ron.xaday.XaDayIntentService.onHandleIntent(XaDayIntentService.java:84)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err:     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err:     at android.os.Looper.loop(Looper.java:211)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:61)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err:     at libcore.io.Posix.open(Native Method)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:451)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err:   ... 12 more
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: java.io.FileNotFoundException: /storage/sdcard1/DCIM/thai2/IMG-20150702-WA0001.jpg: open failed: EACCES (Permission denied)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:465)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at xaday.ofek.ron.xaday.FileUtils.move(FileUtils.java:36)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at xaday.ofek.ron.xaday.XaDayIntentService.handleActionMove(XaDayIntentService.java:136)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at xaday.ofek.ron.xaday.XaDayIntentService.onHandleIntent(XaDayIntentService.java:84)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at android.os.Looper.loop(Looper.java:211)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:61)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at libcore.io.Posix.open(Native Method)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:451)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err:   ... 9 more

Just to make sure I was not locking the file in some way, I used another file manager app to execute the same file move - and it worked! why is it not working for me?!

anyone has an idea?

Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • Is the SDCard mounted? You can check with if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) – LBes Nov 13 '15 at 09:28
  • Yes i made sure it is mounted... – Ofek Ron Nov 13 '15 at 09:29
  • one of them is 5.1 and the other is 4.4.2 – Ofek Ron Nov 13 '15 at 09:32
  • Can you access the image with adb shell? Are the phones rooted? I'm afraid I fail to understand why you get this error... – LBes Nov 13 '15 at 09:37
  • I didnt try, but i can access the image with BitmapFactory.decode() with the exact same path, non of the phones are rooted – Ofek Ron Nov 13 '15 at 09:38
  • Did you put the `` in the right place in Manifest? Could you check if the permission is granted by using `Contex.checkCallingOrSelfPermission(permission)` ? – Henry Nov 13 '15 at 10:16
  • yes, just checked that, got the premissions and still doesnt work – Ofek Ron Nov 13 '15 at 10:26
  • Are you trying to write in a root folder? – Nanoc Nov 13 '15 at 10:29
  • no, /storage/sdcard1/DCIM/thai2/ is definitly not a root folder, also note that another app COULD execute the same file transfer i was trying to do... – Ofek Ron Nov 13 '15 at 10:32
  • What is the other app that can execute the same file transfer? – Mattia Maestrini Nov 18 '15 at 06:50
  • Could you show how you get the source and destination path ? – W3hri Nov 18 '15 at 09:43
  • Can you try disabling file transfer from device to computer. If u enable file transfer, The SD card is not accessible whilst you are debugging your application. Also try DISABLING Protect USB storage under Developer options in the Settings app on a device running Android 4.1 or higher. – apmartin1991 Nov 18 '15 at 14:13
  • @MattiaMaestrini the file explorers that i have on the devices – Ofek Ron Nov 18 '15 at 15:05

2 Answers2

5

On Android Kitkat (4.4), Google has changed how the developers can access to the removable storage (SD card). Quoting from source.android.com:

The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external storage on a device. Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions. Restricting writes in this way ensures the system can clean up files when applications are uninstalled.

For this reason, even with the WRITE_EXTERNAL_STORAGE permission, you can't write on the SD card on KitKat and higher versions.

On Android Lollipop (5.0), Google adds a new set of API that allows the developers to manage the file on the SD Card through the Storage Access Framework.

This StackOverflow answer explains in detail how to use the new APIs and the existing limitations:

How to use the new SD card access API presented for Android 5.0 (Lollipop)?

Community
  • 1
  • 1
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
0

Is the "uses-permission" tag outside of the "application" tag in the manifest.xml?

Luca Ziegler
  • 3,236
  • 1
  • 22
  • 39