gpt4 book ai didi

java - 我可以从drawable或assets之外的任何其他文件夹加载文件吗?

转载 作者:行者123 更新时间:2023-12-01 11:05:25 24 4
gpt4 key购买 nike

我知道Android中的大多数文件访问方法通常是通过访问drawable(使用R.drawable..)或某些 Assets (使用AssetManager)来执行的。

无论如何,我可以访问除上述文件夹之外的任何其他文件夹,例如我的 android 项目名称下的 myfolderA 或 myNewFolderA 吗?

如果可能的话,我想其他方式可能不被认为是正常的,但我只是想知道这是否可能。

最佳答案

Can I access to any other folders than those above just like myfolderA or myNewFolderA under my android project name?

您可以按照 Android Documentation 中的说明进行操作:

<小时/>

Android 使用的文件系统与其他平台上基于磁盘的文件系统类似。您可以使用 Android 文件系统来读取和写入文件 File APIs .

A File object is suited to reading or writing large amounts of data in start-to-finish order without skipping around. For example, it's good for image files or anything exchanged over a network.

<小时/>

此外,您还必须小心读取/写入文件的位置:

内部和外部存储

All Android devices have two file storage areas: "internal" and "external" storage. These names come from the early days of Android, when most devices offered built-in non-volatile memory (internal storage), plus a removable storage medium such as a micro SD card (external storage). Some devices divide the permanent storage space into "internal" and "external" partitions, so even without a removable storage medium, there are always two storage spaces and the API behavior is the same whether the external storage is removable or not. The following lists summarize the facts about each storage space.

<小时/>

将文件保存到内部存储
将文件保存到内部存储时,您可以获取相应的目录 File通过调用以下两种方法之一:

getFilesDir()

Returns a File representing an internal directory for your app.

getCacheDir()

Returns a File representing an internal directory for your app's temporary cache files. Be sure to delete each file once it is no longer needed and implement a reasonable size limit for the amount of memory you use at any given time, such as 1MB. If the system begins running low on storage, it may delete your cache files without warning.

To create a new file in one of these directories, you can use the File() constructor, passing the File provided by one of the above methods that specifies your internal storage directory. For example:

File file = new File(context.getFilesDir(), filename);
<小时/>

外部存储的权限:
为了允许您读取和写入,您必须授予权限:

<manifest ...>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

关于java - 我可以从drawable或assets之外的任何其他文件夹加载文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33012079/

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