Android ::我可以从可绘制文件或资产之外的任何其他文件夹加载文件吗? [英] Android :: Can I load files from any other folders than drawable or assets?

查看:53
本文介绍了Android ::我可以从可绘制文件或资产之外的任何其他文件夹加载文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Android中大多数文件访问方法通常都是通过访问drawable(使用R.drawable ..)或某些资产(使用AssetManager)来实现的.

I know most of file-accessing methods in Android are in general performed by accessing into drawable(using R.drawable..) or, some, assets(using AssetManager).

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

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

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

If possible, I guess those other ways might not be considered normal, but I just want to know if that is possible or not. Thx.

推荐答案

除了我的Android项目名称下的 myfolderA myNewFolderA 之外,我还能访问除上述文件夹以外的任何其他文件夹吗?

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

,您可以按照 Android中的说明进行操作文档:

Android 使用的文件系统与其他平台上基于磁盘的文件系统类似.您可以与 Android 文件系统一起使用

Android uses a file system that's similar to disk-based file systems on other platforms. You can work with the Android file system to read and write files with the File APIs.

A 文件 对象适合于以从头到尾的顺序读取或写入大量数据,而不会跳过.例如,它非常适合图像文件或通过网络交换的任何内容.

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.


此外,您还必须注意在何处读取/写入文件:


Also you must be carefull about where you read/write your files:

内部和外部存储

所有Android设备都有两个文件存储区:内部"和外部"存储.这些名称来自Android的早期,当时大多数设备都提供内置的非易失性内存(内部存储)以及可移动存储介质,例如micro SD卡(外部存储).某些设备将永久存储空间划分为内部"和外部"分区,因此即使没有可移动存储介质,也始终有两个存储空间,并且无论外部存储是否可移动,API行为都是相同的.以下列表总结了有关每个存储空间的事实.

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()

返回一个文件,该文件代表应用程序的临时缓存文件的内部目录.请确保在不再需要每个文件后将其删除,并对您在任何给定时间使用的内存量实施合理的大小限制,例如1MB.如果系统存储空间开始不足,可能会在没有警告的情况下删除您的缓存文件.

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.

要在这些目录之一中创建新文件,可以使用 File()构造函数,并传递由上述方法之一指定的 File 内部存储目录.例如:

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);


权限用于外部存储:
为了允许您读写,您必须授予权限:


Permissions For externa storage:
in order to allow you read and write you must give permissions:

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

这篇关于Android ::我可以从可绘制文件或资产之外的任何其他文件夹加载文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆