我写这个是为了在SD卡中存储一个文件,但它不起作用。
public class TestarFilesActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File root = Environment.getExternalStorageDirectory();
try {
if (root.canWrite()) {
File file = new File(root, "agora.txt");
FileWriter fwriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fwriter);
out.write("Hello world");
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}TestarFilesActivity.this.finish();
}
}
这个问题是怎么引起的,我该如何解决?我认为一切都很好,我也在清单上写下了许可。
如果你在android模拟器中运行这个应用程序,你必须通过DevTools终端模拟器准备sdcard,并使用linux命令检查sdcard目录为$ls,并允许使用sdcard如果不可用,请准备并尝试以下
File file = new File(root, "/sdcard/agora.txt");
FileWriter fwriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fwriter);
out.write("Hello world");
out.close();
//Use this in manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
您是否在AndroidManifest.xml中授予了对SD卡的写入权限?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />