目录和放大器;文件创建在Android文件系统 [英] Directory & File Creation on Android File System

查看:122
本文介绍了目录和放大器;文件创建在Android文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个目录层次结构,然后将文件从我的资产复制到新创建的目录。我用这里提供计算器上的code样品,但我没有得到任何地方。基本上我的code是这样的:

I'm trying to create a directory hierarchy and then copy a file from my assets into the newly created directory. I've used the code samples offered here on stackoverflow but I'm not getting anywhere. Essentially my code goes like this:

InputStream in = getAssets.open(mydb.sqlite);
File dir = new File("/data/data/my.package/databases/");

dir.mkdirs();
out = new FileOutputStream("/data/data/my.package/databases/mydb.sqlite");
while ((len = in.read(buffer)) > 0)
{
    out.write(buffer, 0, len);
}
out.flush();
out.close();
in.close

这是所有的主要活动中首次推出,当然只能做一次完成。在code运行之前,存在的唯一目录是/数据/。是的,这是一个数据库文件,但不应该在这一点重要,因为我只是复制文件,但尚未试图与数据库code来访问它。

This is all done within the main activity on first launch and of course should only be done once. Before the code runs, the only directory that exists is /data/. Yes, it is a database file but that shouldn't matter at this point since I'm just copying the file and haven't yet tried to access it with the database code.

在code不抛出任何异常,dir.mkdirs()返回true。无论如何,没有创建的目录和文件不会被复制。我已经加入了WRITE_EXTERNAL_STORAG​​E权限行到我的表现为已在几个地方,FWIW被建议。我可以通过code步骤,一切似乎都很正常。但是,当然,它不是。

The code doesn't throw any exceptions, and dir.mkdirs() returns true. Regardless, the directories are not created and the file is not copied. I have added the permissions line for WRITE_EXTERNAL_STORAGE to my manifest as has been suggested in several places, FWIW. I can step through the code and everything appears to be fine. But of course it's not.

什么我需要寻找在这里?文件权限的问题?为什么code认为一切都在那里(即,当它到达副本code,它不会抛出FileNotFoundException异常),但在现实中有什么文件系统?

What do I need to be looking for here? File permissions problems? Why does the code think everything is there (ie, when it gets to the copy code, it doesn't throw FileNotFoundException) but in reality there's nothing on the file system?

修改:这就是我所学到的。 GETDIR()创建于/data/data/my.package/app_*的目录。不知道为什么它使用的目录名称此preFIX。如果你需要把一些在标准位置,如数据库/或文件,这将不起作用/.

Edit: Here's what I've learned. getDir() creates the directory in /data/data/my.package/app_*. Not sure why it uses this prefix on the directory name. This won't work if you need to put something in a standard location such as databases/ or files/.

推荐答案

使用openFileOutput不是作为/数据/数据​​是内部存储,你应该只使用Android API来获取文件句柄那里。请参阅:的http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Use openFileOutput instead as /data/data is internal storage and you should only use the Android APIs to get a file handle there. See: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

编辑:
/data/data/my.package由openFileOutput / GETDIR处理,这样你只会创建GETDIR('数据库',MODE_PRIVATE)目录有创建文件。

/data/data/my.package is handled by openFileOutput/getDir so you would create your directory with getDir('databases',MODE_PRIVATE) and create your file there.

EDIT2:
从GETDIR文档:

From getDir documentation:

您可以使用返回的文件对象创建和访问文件,此目录中。

You can use the returned File object to create and access files in this directory.

这篇关于目录和放大器;文件创建在Android文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆