Eclipse中读写SD卡数据

发布于 2024-12-19 05:59:43 字数 411 浏览 0 评论 0原文

可能的重复:
从 SD 卡读取和写入文件

我是一部分这是一个小组项目的一部分,我们必须在 Eclipse 中开发我们自己的 Android 应用程序。 我的项目部分要求我能够读取和写入 SD 卡数据。我已经能够成功读取和写入内部存储上的文件,但我很难在 SD 卡上执行此操作。 我浏览过的所有网站都不是很有用。我是 Eclipse 的新手,所以我对它不太了解。

我想知道是否可以获得有关如何对 SD 卡进行某种流设置的分步说明。我所要做的就是将文本存储到文件中。 谢谢!

Possible Duplicate:
Reading and Writing a file from SD card

I am a part of a group project where we have to develop our own Android app in Eclipse.
My part of the project requires that I be able to read and write data to the SD card. I have been able to successfully read and write to files on the internal storage, but I'm having a hard time being able to do it to the SD card.
All of the websites I've looked at have not been very useful. I am brand new to Eclipse so I don't understand much about it.

I was wondering if I could get step by step instructions on how to get some kind of stream setup to the SD card. All I have to do is simply store text to files.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

骄兵必败 2024-12-26 05:59:43

您是否在manifest.xml中使用了权限元素?

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

do you used permissions elements in manifest.xml?

<uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
零時差 2024-12-26 05:59:43

尝试这种方式将txt文件写入SD卡,从SD卡读取文件非常相似,

File getExternalDir(String dirName) throws IOException {
    File dir = new File(Environment.getExternalStorageDirectory(), dirName);

    if (!dir.exists()) {
        dir.mkdirs();
    }

    return dir;
}


void saveFile(){

File txtFile = new File(getExternalDir(cacheDirectory), fileName);

// write the txt file to SD card
FileOutputStream outputStream = new FileOutputStream(txtFile, true);
OutputStreamWriter writer = new OutputStreamWriter(outputStream);

writer.write(result.toString());
writer.flush();
writer.close();
}

try this way to write the txt file to SD card, reading file from SD card is very similar,

File getExternalDir(String dirName) throws IOException {
    File dir = new File(Environment.getExternalStorageDirectory(), dirName);

    if (!dir.exists()) {
        dir.mkdirs();
    }

    return dir;
}


void saveFile(){

File txtFile = new File(getExternalDir(cacheDirectory), fileName);

// write the txt file to SD card
FileOutputStream outputStream = new FileOutputStream(txtFile, true);
OutputStreamWriter writer = new OutputStreamWriter(outputStream);

writer.write(result.toString());
writer.flush();
writer.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文