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

标签 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());

最佳答案

我通过获取文件的 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/

相关文章:

android - CreateTempFile 在图片文件夹中返回没有这样的文件或目录?

android - 如何在您的应用程序项目中以编程方式创建目录?

android - 来自 Intent.ACTION_OPEN_DOCUMENT 提供的 Android URI 的文件上次修改时间

android - 将多种产品 flavor 与 flavor 维度组合时,文件 google-services.json 丢失

android - 如何在其中的文本更改时修复 TextView 位置

android - 如何在 webview android 4.4 及更高版本中启用多个文件上传

android - 如何从 MediaStore.File 和 MediaStore.File.FileColumn 获取数据

Android textview 测试在其他 UI 上

java - 路径 "file:///android_asset/"记录在哪里?

android - 从 Android 应用程序直播到 Youtube