简体   繁体   English

android-从/ res / raw文件夹中检索所有文件作为文件类型

[英]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.

相关问题 将目录和文件从res / raw文件夹复制到SD卡 - android - Copying directories and files from res/raw folder to sd card - android 从res / raw文件夹中读取文本文件 - Reading a text file from res/raw folder 尝试从Android Studio中的res-> raw文件夹获取视频uri时找不到文件异常 - File not found exception while trying to get the video uri from res->raw Folder in Android Studio Android:按文件名播放/res/raw 中的音频文件 - Android: playing audio files in /res/raw by file name 用于从res / raw播放.ogg文件的Android Media Player错误(-4 -4) - Android media player error (-4 -4) for playing .ogg files from res/raw res/raw 文件夹中文件的完整路径 - Full path to files in res/raw folder 从/ res / raw中的文本文件填充android Fragment中的文本视图 - populating a text view in an android Fragment from a text file in /res/raw Android:OnClick播放/ res / raw文件中的随机.mp3 - Android: OnClick play random .mp3 from /res/raw file 如何访问res / raw文件夹或android中任何其他位置的文件? - How can I access files in res/raw folder or any other location in android? 从/ res / raw /检索列表数据并将其存储到Android上的2D数组的最快方法 - Fastest way to retrieve tabulated data from /res/raw/ and store it into a 2D array on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM