3

i am new to android. i have tried this code

in manifest file i wrote this

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

in java code

   File sdcard = Environment.getExternalStorageDirectory();
    //Get the text file
    File file = new File(sdcard,"store.txt");

but i am getting above exception

i want to perform file read and write operations and i am using version 4.4.2.

i am getting this error

06-14 02:19:44.491: D/Shopping Cart(1700): open failed: EACCES (Permission denied)
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
user3201607
  • 79
  • 1
  • 1
  • 9

2 Answers2

3

Under kitkat your app can only write to an app specific directory on the -removable- sd card. You get possible directories using getExternalFilesDirs().

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • It works with Android 4.4 on a Nexus 7 but it does not on a Galaxy S4, I'm getting crazy with this problem. – David Nov 07 '14 at 17:00
2

Is your permission is right place in manifest file as

    <application>
        ...

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

Edit

Try to edit the Emulator

  1. go to android virtual device manager and then edit the emulator
  2. set say 100 MB for Sd card for the respected emulator and say Ok
  3. save and close emulator and start
  4. path saved in DDMS is mnt/sdcard/yourfilename

it worked for me the app is not giving Error and is working

Please see more at this link Cannot Write to sdcard in Android emulator and this link Guide to Emulating an SD Card Using the Google Android Emulator

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74