在android中获取预加载的文件

时间:2013-04-07 22:58:35

标签: android android-file

我是Android开发的新手,我正在开发一个小项目。我遇到的一些问题是访问预加载的文件。

在我的应用程序中,我有一个我预加载的XML文件(我只是将它放在包中的src文件夹中)。我如何在课堂上访问它们?我需要获取指向此文件的File对象,以便我可以像使用I / O文件一样使用它。看起来这应该是微不足道的,但唉,我被卡住了。

让我们说该文件位于:com.app.preloadedFiles / file1.XML

我已经尝试过这方面的事情,但没有成功:

URL dir_url = ClassLoader.getSystemResource("preloadedFiles/file1.XML");
FIle file = new File(dir_url.toURI());

1 个答案:

答案 0 :(得分:1)

我通过在文件中输入一个InputStream来解决这个问题 - 例如:

myContext.getAssets().open(fileName);
//read the data and store it in a variable

然后,如果您确实需要对其进行File相关操作,可以将其写入私有(或公共)目录,并从新编写的文件中执行操作。类似的东西:

File storageDir = myContext.getDir(directoryName, Context.MODE_PRIVATE);
File myFile = new File(storageDir + File.separator + fileName);
//then, write the data to the file and manipulate it -- store the name for access via File later