hyrulelink16 hyrulelink16 - 1 year ago 143
Java Question

Camera directory null

I have problems reading images path url from my sd card.

It always return this error : Attempt to get length of null array: Attempt to get length of null array

Browsing images on my phone I get this directory as a camera roll directory :


I put in my manifest file permissions:

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

This is my code :

public void processImages() {
listOfImages = new ArrayList<>();

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if (isSDPresent) {

//returns same path - /storage/emulated/0/DCIM/Camera/
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()+"/Camera/";
File f = new File(path);

listOfImages = scanPhotos(f);

} else {
Toast.makeText(this, "sd not present", Toast.LENGTH_SHORT).show();

public List<File> scanPhotos(File directory) {
List<File> listOfPictures = new ArrayList<>();

//this returns null
File[] files = directory.listFiles();
for (File file : files
) {
if (file.isDirectory() && !file.isHidden()) {
} else {
if (file.getName().endsWith(".jpg") || file.getName().endsWith(".jpeg") || file.getName().endsWith(".png")) {

return listOfPictures;


f.canRead() - returns false

Answer Source

Posting my comment as answer. From marshmallow onwards you need to provide Runtime permissions.

There is a callback

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {}

which tell permission is granted by user or not.

For adding Request permission use this tutorial.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download