sd.canWrite()总是返回false [英] sd.canWrite() always returns false

查看:1210
本文介绍了sd.canWrite()总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置下面的我的Andr​​oid清单:

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返回到/ mnt / SD卡

sd returns /mnt/sdcard

我测试我的设备(EVO 3D)不是仿真器上。 SD卡安装。它发生时,双方负责only模式和USB数据线未插好。堆栈显示在谷歌的各种网页和帖子完全相同的方法来检查SD卡是可写的,所以我必须失去了一些东西。

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.

更新:

我试试下面的code和它抛出IOException异常权限被拒绝。

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;
    }

我已经格式化我的SD卡,重新安装它,并重新启动手机,仍然有同样的错误。

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

更新

显然,这是我自己的无知,导致它失败。我有我的权限在我的清单中的错误的部分阻塞。再次:)谢谢

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 :)

推荐答案

我不会用sd.canWrite(),实际上,我会用这样的:

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);
}

从这里摘自:HTTP://www.xinotes.org/notes/note/1270/

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

这篇关于sd.canWrite()总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆