Java 创建字符串输出到SD卡上可访问的文件时出现问题
这是我在这里的第一篇文章,我应该建议,我的知识充其量是有限的 我正在尝试为我的android手机创建一个应用程序 我需要最终将用户输入的值输出到一个文件中,很可能是一个txt文件,当通过usb电缆连接时,可以直接从手机中检索 然而,我正在努力理解如何实现这一点 据我所知,我需要确保卡已安装, 要获得路径, 然后将FileOpenStream()与FileWriter一起使用 将字符串“加载”到文件中(很抱歉,不确定如何正确地描述该字符串,或者其他相关内容) 我还允许清单中的允许写入SD卡。然而,我能找到的所有Exmaple似乎都与SD卡无关。感谢您的建议和/或指导 我现在的代码到处都是,因为我把自己弄糊涂了,对不起Java 创建字符串输出到SD卡上可访问的文件时出现问题,java,android,Java,Android,这是我在这里的第一篇文章,我应该建议,我的知识充其量是有限的 我正在尝试为我的android手机创建一个应用程序 我需要最终将用户输入的值输出到一个文件中,很可能是一个txt文件,当通过usb电缆连接时,可以直接从手机中检索 然而,我正在努力理解如何实现这一点 据我所知,我需要确保卡已安装, 要获得路径, 然后将FileOpenStream()与FileWriter一起使用 将字符串“加载”到文件中(很抱歉,不确定如何正确地描述该字符串,或者其他相关内容) 我还允许清单中的允许写入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">
我对您的代码进行了一些清理,并更改了几行,我认为它现在可以满足您的要求。我使用缓冲写入器写入SD卡。 您可以阅读有关我使用的BufferedWriter的更多信息
我找到了我的XML权限问题的答案,现在它可以工作了
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
感谢您帮助编写文件代码。那么您有什么问题?有什么东西失败了吗?你有例外吗?在那一点上,我的问题是不知道该做什么,或者怎么做!非常感谢您对代码的帮助!(我甚至认为我理解这是一个额外的;)。但是我有一个filenotfound异常被抛出,所以想知道我的清单文件是否有权限问题。我将尝试在我的原始帖子中发布相关的XML。@user1096365您是否已将此权限添加到清单中?看来你是根据你的职位,但是这应该是唯一需要的许可,所以考虑双重检查。顺便说一句,欢迎来到StackOverflow:-)看来我们交叉发布了,你是对的-这是我清单中需要的,奇怪的是,不是eclipse编辑到文件中的内容!(我想我通过Eclipse做了这个错误)如果现在一切都在工作,请考虑接受我的回答:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>