0

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

Community
  • 1
  • 1
Kapparino
  • 988
  • 11
  • 33

1 Answers1

2

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)

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ok, so that is how it is. However (the following question may be off-topic), but main problem, or what I'm trying to achieve, is that I need to set path for `new MediaMetadataRetriever().setDataSource()` in order to collect some data regarding that mp3 file. How do I get that path and set that value to `setDataSource(path)` ? – Kapparino Apr 24 '16 at 12:07
  • @Kapparino: There are six versions of `setDataSource()` on [`MediaMetadataRetriever`](http://developer.android.com/reference/android/media/MediaMetadataRetriever.html), taking different parameters. If nothing else, use the one that takes a `MediaDataSource`, then implement a `MediaDataSource` that wraps your desired resource or asset. Also, for any of the methods on `Resources` or `AssetManager` that return an `AssetFileDescriptor`, you can call `getFileDescriptor()` on that to get a `FileDescriptor` to pass into `setDataSource()`. – CommonsWare Apr 24 '16 at 12:12
  • Hi again, sorry for disturbing, can you please check following image: `http://i.imgur.com/9VQ6UdE.png` I'm getting null for data as you can see on the image, although values are actually set for file. – Kapparino Apr 24 '16 at 14:02
  • @Kapparino: I have no idea, sorry. – CommonsWare Apr 24 '16 at 14:08