javaandroidandroid-studioandroid-file

Not able to get files from any Android folder


I am new in android and was trying to test something. I have this simple app, where I just want to read files from any android folder. Till now, I just checked how the paths are there which I came to know from the following

context.getExternalFilesDir(null);

&

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

The paths obtained are like

/storage/emulated/0/ then rest of the folders

I have done everything, asked for permission in the manifest file, and in the app too. I check that even though the permission is granted and folder path is also correct but the File.listFiles() is givingme null pointer exception.

Please take a look at the code below:

Permission in manfest file

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

Code to get permission and try checking the files

 if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},101);
        Toast.makeText(getApplicationContext(),
                "No permission yet",
                Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(getApplicationContext(),
                "Permission already granted",
                Toast.LENGTH_SHORT).show();
       
        File dcimPath = new File("/storage/emulated/0/bluetooth"); //This path is correct
        
        File[] files = dcimPath.listFiles(); // gives null pointer exception
        Log.i("files length ",files.length+"");
    }

     @Override
     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == 101){
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(getApplicationContext(),
                    "Permission granted",
                    Toast.LENGTH_SHORT).show();
            File dcimPath = new File("/storage/emulated/0/bluetooth/"); 

            File[] files = dcimPath.listFiles(); //gives error
            Log.i("files length ",files.length+"");
        }
        else{
            Toast.makeText(getApplicationContext(),
                    "Permission denied",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

ERROR

 Caused by: java.lang.NullPointerException: Attempt to get length of null array

Solution

  • Try adding this line to <application> tag on the Android Manifest File

            android:requestLegacyExternalStorage="true"
    

    This is used to read Shared Storage apart from the app's private storage. Read this if you are curious to know more...

    Hope this helps. Feel free to ask for clarifications...