2

if(new File("/mnt/sdcard/t.m").exists()) {...}

When I create a java program using Eclipse and debug it on my Android phone, it tests right. But when I put it into a app in my Android system, it does not detect the file.

And, in another app (built in my Android system, too), though I've put following in AndroidManifest.xml,

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

the code below causes an exception saying no permission,

File f=new File("/mnt/sdcard/a.t"); f.createNewFile();

Thank you. I'm new to Android.

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
A117
  • 499
  • 1
  • 5
  • 13

1 Answers1

1

in Permission to write to the SD card:

You're right that the SD Card directory is /sdcard but you shouldn't be hard coding it. Instead, make a call to Environment.getExternalStorageDirectory() to get the directory:

File sdDir = Environment.getExternalStorageDirectory(); If you haven't done so already, you will need to give you app the correct permission to write to the SD Card by adding this to your Manifest:

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

Just change it to read (instead of write)

Community
  • 1
  • 1
Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
  • I had tried all your code. Not working. the file can be listed out by adb shell,so hard coding cannot make it wrong. – A117 Nov 26 '10 at 10:36
  • I'm sorry, I was in a hurry and didn't read properly! Go here and check the state of the storage: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal – Pedro Loureiro Nov 26 '10 at 10:43
  • Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) and I had checked this. The small testing program works verified it. The problem is the difference between my larger program by "make" and the smaller program by Eclipse. – A117 Nov 26 '10 at 11:15