开发者

sd.canWrite() always returns false

I've set the following my android manifest:

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

I've got the following in my activity:

File sd = Environment.getExternalStorageDirectory();
boolean can_write = sd.canWrite();

sd returns /mnt/sdcard

I'm testing on my device (evo 3d) not the emulator. The sd card is mounted. It happens both when in charge only mode and when the usb cable isn't plugged in. Various pages on google and posts on stack show that exact same method to check if the sd card is writable so I must be missing something.

UPDATE:

I try the following code and开发者_高级运维 it throws ioexception permission denied.

    File TestFile = new File(sd + "/testfile.txt");
    try {
        boolean result = TestFile.createNewFile();
        if(result)
        {
            Log.i("tag","successfully created file");
        } else {
            Log.i("tag","failed to create file");
        }
    } catch (IOException e) {
        Log.e("tag",e.toString() + " " + TestFile);
        return false;
    }

I've formatted my sdcard, re-mounted it, and rebooted phone and still have the same error.

UPDATE

Apparently it's my own ignorance that caused it to fail. I had my permissions block in the wrong section of my manifest. Thanks again :)


I wouldn't use sd.canWrite(), actually, I would use something like this:

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
    Log.d("Test", "sdcard mounted and writable");
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    Log.d("Test", "sdcard mounted readonly");
}
else {
    Log.d("Test", "sdcard state: " + state);
}

Taken from here:http://www.xinotes.org/notes/note/1270/


canWrite() method shows false for directories by default this is as per my observation do not know the reason behind it

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜