使用文档中的数据存储页面,我试图将一些数据存储到SD卡中.这是我的代码:
// Path to write files to
String path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/"+ctxt.getString(R.string.package_name)+"/files/";
String fname = "mytest.txt";
// Current state of the external media
String extState = Environment.getExternalStorageState();
// External media can be written onto
if (extState.equals(Environment.MEDIA_MOUNTED))
{
try {
// Make sure the path exists
boolean exists = (new File(path)).exists();
if (!exists){ new File(path).mkdirs(); }
// Open output stream
FileOutputStream fOut = new FileOutputStream(path + fname);
fOut.write("Test".getBytes());
// Close output stream
fOut.flush();
fOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
当我创建新的FileOutputStream时,我得到一个FileNotFound异常.我也注意到"mkdirs()"似乎没有创建目录.
谁能告诉我我做错了什么?
我正在使用2GB SD卡和"hw.sdCard:yes"在AVD上进行测试,Eclipse中DDMS的文件浏览器告诉我,SD卡上唯一的目录是"LOST.DIR".
您是否已将您的申请许可写入SD卡?
您可以通过添加以下内容来AndroidManifest.xml执行此操作:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)