0

I wolud like to read entire raw project folder content (that contains some .mp3 files). This is how i try to read folder content:

Uri uri = Uri.parse("android.resource://" + this.getPackageName() + "/raw/");
    File file = new File(uri.getPath());
    File[] files = file.listFiles();
    for (int i = 0; i < files.length; i++) {
        Log.i("ADASDAS", files[i].getName());
    }

But i obtain a NullPointerException on files object. What's wrong? The exception is on files.length call.

giozh
  • 9,868
  • 30
  • 102
  • 183

1 Answers1

0

What's wrong?

Pretty much everything:

  • a Uri is not a File, so you cannot pass getPath() to File and expect it to work

  • resources are not files on the device, but rather are entries in an APK file, and so you cannot iterate over them with File objects

  • the android.resource Uri structure points to resources, not types of resources (e.g., a specific raw resource)

There really isn't a great way to iterate over raw resources. The best not-great way is to use Java reflection to iterate over all R.raw fields.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491