8

I am using code below to get all images from Camera folder inside DCIM and display in my app. But I want to display all images on the device in my app regardless of where they are stored on the device. How can I do this?

 String ExternalStorageDirectoryPath = Environment
                .getExternalStorageDirectory()
                .getAbsolutePath();
        String targetPath = ExternalStorageDirectoryPath + "/DCIM/Camera";
        images=new ArrayList<String>();
        File targetDirector = new File(targetPath);

        File[] files = targetDirector.listFiles();
        for (File file : files) {

            images.add(file.getAbsolutePath());
        }
        gallerylist=new CameraGalleryAdapter(getActivity().getApplicationContext(),R.layout.giphy_grid,images);
        gridview.setAdapter(gallerylist);
Harshad Kale
  • 17,446
  • 7
  • 30
  • 44
Android Developer
  • 9,157
  • 18
  • 82
  • 139

1 Answers1

16

First of all check if required permissions are granted:

if (ActivityCompat.checkSelfPermission(CurrentActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
     ActivityCompat.requestPermissions(CurrentActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICK_FROM_GALLERY)
}

Then you have to check if SD card is available:

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

If SD card is present then use this:

final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
//Stores all the images from the gallery in Cursor
Cursor cursor = getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
        null, orderBy);
//Total number of images
int count = cursor.getCount();

//Create an array to store path to all the images
String[] arrPath = new String[count];

for (int i = 0; i < count; i++) {
    cursor.moveToPosition(i);
    int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
    //Store the path of the image
    arrPath[i]= cursor.getString(dataColumnIndex);
    Log.i("PATH", arrPath[i]);
}
// The cursor should be freed up after use with close()
cursor.close();

The above code will list all images from SD card, For getting Images from Internal Memory just replace

MediaStore.Images.Media.EXTERNAL_CONTENT_URI

with

MediaStore.Images.Media.INTERNAL_CONTENT_URI

Using the same snippet of code.

Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
  • What is the best practice to get all "photo" images only? Currently i list the files directly from DCIM folder, then get the paths, but i wonder if there is a best practice to do that. – HendraWD Nov 08 '16 at 07:22
  • @HendraWijayaDjiono There is no "common best practice". If you notice the way images are fetched no longer works consistently on Marshmallow devices. Please post a separate question or maybe someone already has. And then let me know, I'll put my 2 cents in it. – Sharp Edge Nov 08 '16 at 17:26
  • not working in 5.0. kindly review it again it need some help :) – Zaeem Sattar Mar 29 '17 at 10:59
  • @zaeemsattar yes it might not work now, and this answer is probably outdated, Thanks for your comment I'll update it. – Sharp Edge Mar 29 '17 at 12:01
  • 1
    @SharpEdge Thanks – Zaeem Sattar Mar 29 '17 at 12:24