我已经研究这个问题有一段时间了,但我只找到了非常复杂的答案,所以我很困惑。请记住,我不是专家程序员,所以不要指望我对此了解很多!
我想做的就是将新的一行字符打印到位于 Android SD 卡下载文件夹中的文本文件中。我将模拟器设置为具有 SD 卡,并将文本文件放在下载文件夹中。这段代码用于一个数据库类,该类将访问 SD 卡中的文本文件以读取数据。我知道该类在 Android 之外工作,因此假设所有方法都可以正常工作来读取数据!
当我在另一个类中运行此方法时,我收到 IOException:
public void addRecordToDataBase(ChildRecord c) throws IOException
{
FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt");
PrintWriter out = new PrintWriter(outFile);
out.println(c.printToDataBase());
out.close();
}
奇怪的是,我可以使用相同路径在其他方法中很好地从数据库中读取数据;那里没有问题。我只是无法写信给它。我在某处读到过,您可以使用“常规 Java 方法”在 Android 中写入 SD 卡,而无需到处都是那些疯狂的“OutputStream”东西。这是真的吗?我调试了这个东西,发现抛出异常的代码行就在这里:
FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt");
如果有人知道为什么我得到这个 IOException,我将非常感激!我确实尝试了 Android 想要使用的所有疯狂方法,但我想我迷失了其中,所以我只是回到了我知道该怎么做的地方。
太感谢了!
I've researched this problem for a while now and I've only found really complicated answers so I'm very confused. Keep in mind that I'm not an expert programmer so don't expect me to know a ton about this!
All I want to do is print a new line of characters to a text file located in the downloads folder of an SD card in Android. I set up my emulator to have an SD card and placed the text file in the downloads folder. This piece of code is for a database class that will access a text file in an SD card to read the data. I know that the class works outside of Android so assume that all of the methods are working as they should to read the data!
I get an IOException when I run this method in another class:
public void addRecordToDataBase(ChildRecord c) throws IOException
{
FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt");
PrintWriter out = new PrintWriter(outFile);
out.println(c.printToDataBase());
out.close();
}
The weird thing is I can read from the database just fine in other methods using that same path; no problems there. I just can't write to it. I've read somewhere that you can use "regular Java methods" to write to an SD card in Android without those crazy "OutputStream" things all over the place. Is this true? I debugged this thing and found out that the line of code that is throwing the exception is right here:
FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt");
If anyone has any idea why I'm geting this IOException, I would be really grateful! I did try all the crazy methods that Android wants to use but I think I got lost in it so I just reverted back to what I knew how to do.
Thank you so much!
发布评论
评论(2)
我的猜测是您无法以写入权限访问此类目录,至少以这种方式。
您是否看过 http://developer.android.com /guide/topics/data/data-storage.html#filesExternal?
该参考和这个(http:// /developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)),解释 getExternalFilesDir 的工作原理,可能对您有帮助!
My guess is you can't access such directory with writing permissions, at least in that manner.
Did you take a look at http://developer.android.com/guide/topics/data/data-storage.html#filesExternal?
That reference and this one (http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)), explaining how getExternalFilesDir works, may be of help to you!
您是否在应用清单文件中声明了 WRITE_EXTERNAL_PERMISSION 权限?
Have you declared the WRITE_EXTERNAL_PERMISSION permission in your apps manifest file?