3

I am writing a file to SDCard and set <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> it works good upto 3.x but on 4.0 it gives the below error.

java.io.IOException: open failed: EACCES (Permission denied)
at java.io.File.createNewFile(File.java:940)
at com.gt.mLearning.app.VideoViewActivity.copyFile(VideoViewActivity.java:204)
at com.gt.mLearning.app.VideoViewActivity.access$3(VideoViewActivity.java:193)
at com.gt.mLearning.app.VideoViewActivity$Loader.doInBackground(VideoViewActivity.java:61)
at com.gt.mLearning.app.VideoViewActivity$Loader.doInBackground(VideoViewActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at java.io.File.createNewFile(File.java:933)

Any help or suggestion?

rohit

Charles
  • 50,943
  • 13
  • 104
  • 142
Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83

5 Answers5

5

Just guessing but did you probably have your phone still connected to the computer? This error might happen if your sd card is still locked as a usb device. Hope this helped, Tobias

Tobias Reich
  • 4,952
  • 3
  • 47
  • 90
  • Thanks for reply, no its not such. Latter on I discovered that the problem is with device. – Rohit Mandiwal Nov 26 '12 at 23:19
  • I had the same problem and you gave me the answer. Thanks for that! – Chris Feb 14 '13 at 13:54
  • My app worked like a charm on my SGS2, now I began testing on older devices, and with an old HTC without external storage, it broke writing to file (because it was connected to PC). Disconnecting the phone from the pc did the job. – Twinone Mar 26 '13 at 17:35
1

Probably you have hard-coded the external storage directory and it works on particular devices.

Environment.getExternalStorageDirectory();

Posting your code will help much more than just the log.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
mihail
  • 2,173
  • 19
  • 31
  • Thanks for posting reply dear but I was doing this `Environment.getExternalStorageDirectory().canWrite()` and getting the error :( sad – Rohit Mandiwal Jul 19 '12 at 10:25
  • well, the only thing that comes to my mind. Or the problem might be in the canWrite() method - try using the method from this post - http://stackoverflow.com/questions/6942677/sd-canwrite-always-returns-false – mihail Jul 19 '12 at 11:38
  • looks like the canWrite() method applies for a file, not for a directory. Remove it from your code. – mihail Jul 19 '12 at 11:40
  • Thanks! so if I remove `canWrite()` do I have any other option? – Rohit Mandiwal Jul 19 '12 at 13:25
  • I mean if I omit the `canWrite()` what should I use in that place to check the file write permission? – Rohit Mandiwal Jul 20 '12 at 00:02
  • 1
    check the answer from this post http://stackoverflow.com/questions/6942677/sd-canwrite-always-returns-false – mihail Jul 20 '12 at 08:00
1

Latter on I discovered that the issue is device specific. I installed the File Manager app from Android market and it shows no write permission. It mean the Android OS implementation of that manufacturer do not provide write permission which is causing the issue.

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83
0

You may check you've just updated your Android O/S, too. Some major change cause sdcard problem. For me, I update my Android O/S in SHV-E160K from 2.3.5 to 4.1.2 with Odin which is samsung firmware update application. And I faced problem. My detailed error message is as follows :

02-14 11:00:43.057  22288-22288/? W/System.err﹕ java.io.IOException: open failed: EACCES (Permission denied)
02-14 11:00:43.057  22288-22288/? W/System.err﹕ at java.io.File.createNewFile(File.java:940)
02-14 11:00:43.057  22288-22288/? W/System.err﹕ at com.sec.android.util.IAPDeviceInfoEditor.activity.EditorActivity.createDeviceInfo(EditorActivity.java:222)
02-14 11:00:43.057  22288-22288/? W/System.err﹕ at com.sec.android.util.IAPDeviceInfoEditor.activity.EditorActivity.onClick(EditorActivity.java:121)
02-14 11:00:43.057  22288-22288/? W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-14 11:00:43.057  22288-22288/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
02-14 11:00:43.057  22288-22288/? W/System.err﹕ at android.view.View$1.onClick(View.java:3685)
02-14 11:00:43.057  22288-22288/? W/System.err﹕ at android.view.View.performClick(View.java:4192)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at android.view.View$PerformClick.run(View.java:17248)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:615)
02-14 11:00:43.067      172-485/? V/AudioHardwareMSM8660﹕ open driver
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4950)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at libcore.io.Posix.open(Native Method)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ at java.io.File.createNewFile(File.java:933)
02-14 11:00:43.067  22288-22288/? W/System.err﹕ ... 16 more

The reason is 4.1.2 couldn't recognize file system in 2.3.5. I solve this formatting sdcard storage. format doesn't mean delete your personal information such as address, memo or history. After format, it works like magic.

Dog발자
  • 86
  • 1
  • 6
0

This may help you.

I faced the same issue when writing the file on sdcard. I have set all required permission to write the file but i used file object like below:

Wrong : File myFile = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+fileName);

Correct: File myFile = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), fileName);

That means the path was wrong.

Smeet
  • 4,036
  • 1
  • 36
  • 47