简体   繁体   中英

android - retrieve all files from /res/raw folder as File type

How can I retrieve all the files in /res/raw folder as File? I have found this solution, and "New Folder" value I have replaced with "res/raw" as you can see in the following code:

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 :

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/" ?

How can I retrieve all the files in /res/raw folder as 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM