gpt4 book ai didi

android - 在android中获取预加载文件

转载 作者:行者123 更新时间:2023-11-30 03:40:13 25 4
gpt4 key购买 nike

我是 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());

最佳答案

我通过获取文件的 InputStream 在我的应用程序中解决了这个问题——类似于:

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

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

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

关于android - 在android中获取预加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15868858/

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