1

I'm trying to access files inside my Android phone, and the code below is used for it:

final File folder = new File("/storage/sdcard0/");
        for (final String fileEntry : folder.list()) {
            System.out.println(fileEntry);
        }

It throws a null pointer on list() function, meaning it's not a file/directory. However, if I change the path to /storage/, it prints a bunch of folders including sdcard0. I know sdcard0 actually is a folder/file because I also downloaded a file manager for my phone and went through the files. Many people suggest using getExternalStorageDirectory(), but that results in the same null pointer.

I have added these permissions:

<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

EDIT: one of the many sites that I've looked at android null pointer error

UPDATE: I checked if it was a folder using isDirectory(), and it returns true, then I tried canRead() and it returns false, maybe it's a permission issue?

Community
  • 1
  • 1
Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
  • 1
    There is no such permission as "android.permission.READ_INTERNAL_STORAGE" though that is unlikely to be the issue as the one for reading **EXTERNAL** storage is usually not yet enforced. – Chris Stratton Mar 03 '14 at 06:07
  • that's strange, because ive literally seen 10+ links with people using it, why is it so common? – Brandon Ling Mar 03 '14 at 06:16
  • You (or they) are confusing internal and external. See the documentation for manifest permissions. – Chris Stratton Mar 03 '14 at 12:14

5 Answers5

2

FINALLY I figured it out, if you slide the top menu bar (where you receive notifications) there's "Connected as a media device", if you click that there is more settings, and if you enable "Media device (MTP)", not having this set was automatically unmounting my sdcard when I plugged it in via USB to my computer.

Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
1
final File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
for (final String fileEntry : folder.list()) {
 System.out.println(fileEntry);
}
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
1

Environment.getExternalStorageState() this will give you state of External storage not Path. For checking path please use Environment.getExternalStorageDirectory().getAbsolutePath().

And as per your mentioned sate "shared" it seems you were testing while you accessing it via USB cable into your System. Because "shared" state means "media is present not mounted, and shared via USB mass storage".

Ankit
  • 256
  • 1
  • 9
0

Try getting path to external storage through this:

  System.out.println(Environment.getExternalStorageState());

which returns the path to external storage and cross check it with what you are writing.

pratiti-systematix
  • 792
  • 11
  • 28
0

Can you try this code

final File folder = new File("/storage/sdcard");

    if (folder.getAbsoluteFile().exists()) {
        for (final String fileEntry : folder.getAbsoluteFile().list()) {
            System.out.println(fileEntry);
        }
    }
Shrikant
  • 1,560
  • 1
  • 15
  • 32
  • Can you check that sdcard0 is present inside storage??? because on emulator you have sdcard and not sdcard0. – Shrikant Mar 03 '14 at 06:11
  • i did when i use "/storage/" it gives me a bunch of folders one includes sdcard0 – Brandon Ling Mar 03 '14 at 06:13
  • i have added a check in edited answer to check whether the folder or file exists, because the above code works fine in my emulator and device. – Shrikant Mar 03 '14 at 06:19
  • doesn't do anything, however, i've just noticed a message saying "no sd card mounted" but if i access sdcard0 through my file manager it can read the files – Brandon Ling Mar 03 '14 at 06:25
  • I think I might have figured out why, when i plug my phone to usb through computer it unmounts the sdcard, is this normal? – Brandon Ling Mar 03 '14 at 06:28
  • Might be the cause, check this http://androidforums.com/droid-x-support-troubleshooting/214407-sd-card-unmounts-when-connecting-usb.html and this http://androidforums.com/droid-x-support-troubleshooting/214407-sd-card-unmounts-when-connecting-usb.html If possible you can also check if it is running on emulator. – Shrikant Mar 03 '14 at 06:31