gpt4 book ai didi

java - 将字符串输出创建到 SD 卡上可访问的文件时出现问题

转载 作者:行者123 更新时间:2023-11-30 09:42:50 24 4
gpt4 key购买 nike

这是我在这里的第一篇文章,我应该指出我的知识充其量是有限的!

我正在尝试为我的 Android 手机创建一个应用。

我需要最终将用户输入的值输出到一个文件,很可能是一个 txt 文件,当通过 USB 数据线连接时,可以直接从手机中检索该文件。

但是,我正在努力了解如何实现这一目标。

据我所知,我需要确保卡已安装,要获得路径,然后使用 FileOpenStream() 和 FileWriter将字符串“加载”到文件中(抱歉,不确定描述它的正确方法,或与此相关的任何其他内容)

我还在 list 中允许写入 SD 卡的权限。但是,我能找到的所有示例似乎都与 SD 卡无关。非常感谢收到任何建议和/或指向教程。

我现在的代码到处都是,因为我把自己搞糊涂了,抱歉。

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}

if (mExternalStorageAvailable == true &&
mExternalStorageAvailable == true) {
//Environment.getExternalStorageDirectory() gets root path to SD card
// FileOutputStream to output somehow
File path = Environment.getExternalStorageDirectory();
OutputStream out = null;
try {
//File path = Environment.getExternalStorageDirectory();
//FileOutputStream f = new FileOutputStream(new File(path + "/DCIM", "fileName"));

out = new BufferedOutputStream(new FileOutputStream(file));

OutputStreamWriter osw = new OutputStreamWriter(f);

osw.write("test txt");
osw.flush();// ensure that everything is really written out and close
osw.close();
Toast.makeText(getBaseContext(), "path = " + path, Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "File not FOund", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "IOException", Toast.LENGTH_LONG).show();
}
}

编辑:

这是 XML 文件(希望如此),看看 FileNotFound 异常是否是由它引起的。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidbook.epcsn"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<permission></permission>

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" android:permission="android.permission.WRITE_EXTERNAL_STORAGE">

最佳答案

我稍微清理了您的代码并更改了几行,我认为它现在可以满足您的需求。我使用 BufferedWriter 写入 sdcard。您可以阅读有关我使用的 BufferedWriter 的更多信息 here

    boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}

if ( mExternalStorageAvailable == true && mExternalStorageAvailable == true){

File mFile = new File(Environment.getExternalStorageDirectory(), "/DCIM/filename.txt");
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(mFile, true));
buf.write("test txt");
buf.close();
Toast.makeText(getBaseContext(), "path = "+mFile, Toast.LENGTH_LONG).show();


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "File not FOund", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "IOException", Toast.LENGTH_LONG).show();
}
}

关于java - 将字符串输出创建到 SD 卡上可访问的文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8494843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com