Android应用程序如何在应用程序内部存储和获取文件(Android application how to store and get a file inside the application)

编程入门 行业动态 更新时间:2024-10-28 16:25:43
Android应用程序如何在应用程序内部存储和获取文件(Android application how to store and get a file inside the application)

我有一个应用程序,让我通过文件浏览器选择一个文件(该文件存储在SD上),然后读取它。

我想修改它,因此它将文件直接放入应用程序并从“内部”读取文件。 我必须把文件放到项目中? 我该如何访问它?

I have an application that make me choose a file through a file explorer(the file is stored on the sd), and then reads it.

I want to modify it, so it has the file directly into the app and reads the file from "inside". Where I have to put the file into the project? How can I access it?

最满意答案

是的,您可以将您的文件放入/ assets文件夹,并检索如下:

AssetManager assetManager = getAssets(); InputStream instream = assetManager.open("file.txt");

或res / raw文件夹:

InputStream raw = getResources().openRawResource(R.raw.file);

如果要修改它,您只能将文件写入外部存储(例如sdcard)或内部存储(在应用程序文件夹data/data/package_name/ )。 如果将文件存储到外部存储器中,它将一直存在,直到用户手动或以编程方式删除该文件。 但是,如果将此文件存储到内部存储中,则在用户删除应用程序或清除应用程序缓存时将删除该文件。

Yes, you can put your file into /assets folder, and retrieve as follows:

AssetManager assetManager = getAssets(); InputStream instream = assetManager.open("file.txt");

or res/raw folder:

InputStream raw = getResources().openRawResource(R.raw.file);

If you want to modify it, you'll be able only to write a file into External storage (e.g. sdcard), or into Internal storage (under your application folder data/data/package_name/). If you store your file into External storage it will persist until user manually or programmatically deletes the file. But if you store this file into Internal storage, it will be deleted if user deletes an app, or clear an application cache.

更多推荐

本文发布于:2023-08-01 12:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   文件   如何在   Android   store

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!