开发者

Problem with SD card in Android Emulator QVGA

My application uses a data directory on the SD card to store files. At startup, it creates this directory if it isn't already present. For some reason this works on th开发者_StackOverflowe different AVDs I have set up, except that I can't get it to work if I set the resolution to QVGA.

The code is boring:

File root = new File("/sdcard/mydir");
if(!root.exists()) {
  try {
    root.mkdir();
  }
  catch...

mkdir() is returning false.

Any ideas why?


It's possible that you forgot to set up the SD card when creating the QVGA AVD. Try setting it up again.

Also, you should not be hard coding path to external storage. http://developer.android.com/guide/topics/data/data-storage.html explains some of the APIs to access external storage.

The following snippet might detect some issues with mounting.

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { // Something else is wrong. It may be one of many other states, but all we need // to know is we can neither read nor write mExternalStorageAvailable = mExternalStorageWriteable = false; }


It turns out that I had forgotten to add the SD card permission to my manifest:

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

Without the permission the program worked on all emulators except QVGA, and even on my Droid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜