写文件外部SD卡

时间:2017-04-30 14:27:26

标签: android android-sdcard sd-card

我正在尝试将应用程序的某些输出转储到SD上,以便用户可以通过usb访问它。很简单吧?所以,这就是我的尝试。

File sdCard = Environment.getExternalStorageDirectory();
File dirExt = new File (sdCard.getAbsolutePath() + "/dir1/dir2");
dirExt.mkdirs();
File fileExt = new File(dirExt, "filename");

Log.d("FILE", "Is directory: "+sdCard.isDirectory()+". Exists: "+sdCard.exists()
        +". Can read: "+sdCard.canRead());

Log.d("FILE", "Is directory: "+dirExt.isDirectory()+". Exists: "+dirExt.exists()
        +". Can read: "+dirExt.canRead());

Log.d("FILE", "Is file: "+fileExt.isFile()+". Exists: "+fileExt.exists()
        +". Can read: "+fileExt.canRead());

String string = "Hello!";

try{
    FileOutputStream fos = new FileOutputStream(fileExt);
    fos.write(string.getBytes());
    fos.close();
}catch (Exception e){
    Log.e("FILE", "catch", e);
}

我还在清单文件中包含了写入权限,并从环境中检查了sd状态,它说"已安装"。所以,我们应该好好去。但是,不知何故,我无法访问sd目录中的任何内容,如日志中所述:

4085-4085/com.my.class D/FILE: Is directory: true. Exists: true. Can read: false
4085-4085/com.my.class D/FILE: Is directory: false. Exists: false. Can read: false
4085-4085/com.my.class D/FILE: Is file: false. Exists: false. Can read: false

是否有可能在SD卡上写任何东西?我正在使用具有200 MB SD存储空间的模拟器。

0 个答案:

没有答案