将文件写入 SD 卡不起作用
我写这个是为了将文件存储在 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();
}
}
这个问题是如何引起的以及如何解决?我认为一切都是正确的,我也在清单中写下了许可。
I wrote this to store a file in SD card, but it's not working.
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();
}
}
How is this problem caused and how can I solve it? I think it's everything right and I have write the permission in the manifest too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 android 模拟器中运行此应用程序,则必须通过 DevTools 终端模拟器准备 sdcard 并使用 linux 命令作为 $ls 检查 sdcard 目录并允许 sdcard
如果不可用,请准备好并尝试以下操作
if you run this application in android emulator you have to prepare sdcard through DevTools Terminal Emulator and check sdcard directory using linux command as $ls and allow to sdcard
if not available prepare it and try the following
您是否在 AndroidManifest.xml 中授予了 sdcard 的写入权限?
Have you given write permission on sdcard in your AndroidManifest.xml?