将文件写入 SD 卡不起作用

发布于 2024-11-16 04:02:25 字数 867 浏览 2 评论 0原文

我写这个是为了将文件存储在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看春风乍起 2024-11-23 04:02:25

如果您在 android 模拟器中运行此应用程序,则必须通过 DevTools 终端模拟器准备 sdcard 并使用 linux 命令作为 $ls 检查 sdcard 目录并允许 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" />

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

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" />
ㄖ落Θ余辉 2024-11-23 04:02:25

您是否在 AndroidManifest.xml 中授予了 sdcard 的写入权限?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Have you given write permission on sdcard in your AndroidManifest.xml?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文