[英]android - retrieve all files from /res/raw folder as File type
How can I retrieve all the files in /res/raw folder as File? 如何将/ res / raw文件夹中的所有文件检索为File? I have found this solution, and "New Folder" value I have replaced with "res/raw" as you can see in the following code:
我已经找到了这种解决方案,并且在下面的代码中可以看到,“ New Folder”值已替换为“ res / raw”:
public File[] getRawFiles() {
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "res/raw");
return yourDir.listFiles();
}
When program is started, I get an exception on line return yourDir.listFiles
: 当程序启动时,我在
return yourDir.listFiles
行中return yourDir.listFiles
异常:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
How can I fix this, and what is correct path to "res/raw/" ? 我该如何解决,到“ res / raw /”的正确路径是什么?
How can I retrieve all the files in /res/raw folder as File?
如何将/ res / raw文件夹中的所有文件检索为File?
You cannot do this. 你不可以做这个。 As we discussed yesterday , resources are not files on the filesystem of the device.
正如我们昨天讨论的那样 ,资源不是设备文件系统上的文件。 They are files on your development machine.
它们是开发计算机上的文件。 They are merely entries in an APK file on the device.
它们只是设备上APK文件中的条目。
Either: 要么:
Do whatever you are trying to do some other way that does not involve files, or 以其他方式执行不涉及文件的其他操作,或者
Use openInputStream()
on a Resources
object (you can get one from any Context
via getResources()
), and use Java I/O to copy the contents of a resource to a local file, such as on internal storage , and using reflection to iterate over the actual resources , or 在
Resources
对象上使用openInputStream()
(可以通过getResources()
从任何Context
获取一个),并使用Java I / O将资源的内容复制到本地文件(例如内部存储器)上 ,并使用反射来遍历实际资源 ,或者
Switch to using assets/
instead of res/raw/
, as AssetManager
allows you to list()
assets (though you still only get an InputStream
on an asset, as like resources, assets are not files on the device) 切换到使用
assets/
而不是res/raw/
,因为AssetManager
允许您list()
资产list()
(尽管您仍然仅在资产上获得InputStream
,就像资源一样,资产不是设备上的文件)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.