1

My app worked fine but after a reinstall I got the error that "attempt to get length of null array", while my dob folder is full of files that starting with pi_. Now I get files - Null.

Can anybody help what the error can be?

Thanks

File path = new File(Environment.getExternalStorageDirectory().toString() + "/dob");

    if (path.exists()) {
        Toast.makeText(this, "Dob folder exists", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Dob folder does not exist", Toast.LENGTH_SHORT).show();

    }

    File[] files = path.listFiles();

    if (files != null) {
        Toast.makeText(this, "files length: " + files.length, Toast.LENGTH_SHORT).show();

        piStarters = new String[files.length];
        int db = 0;

        for (File file : files) {
            Toast.makeText(this, ":" + file.toString() + ":", Toast.LENGTH_LONG).show();
            if (file.getName().startsWith("pi_")) {

                piStarters[db] = file.getName();
                Toast.makeText(this, "I need this (pistarter): " + piStarters[db], Toast
                        .LENGTH_SHORT)
                        .show();
                db++;
            }
        }
    } else {
        Toast.makeText(this, "files - Null ", Toast
                .LENGTH_SHORT).show();
    }

Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
johnny cigar
  • 119
  • 10

1 Answers1

1

You required to implement Requesting Permissions at Run Time to access the phone storage otherwise android OS marshmallow and above won't allow your app to access storage

So you need to implement the runtime permission model for storage access

Follow the link for an example

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68