Retrieve all photos from Android devices

I am completely new to Android programming. I am trying to download all photos in an android device on which I am testing my application. I wrote the onCreate function like this -

public void onCreate(Bundle savedInstanceState) {

    ImageAdapter adapter= new ImageAdapter(this);
    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };

    final String orderBy = MediaStore.Images.Media._ID;
    Uri rr= MediaStore.Images.Media.INTERNAL_CONTENT_URI;
    Cursor  imagecursor = getContentResolver().query(
            MediaStore.Images.Media.INTERNAL_CONTENT_URI,null, null,        
            null, orderBy);
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);          
    this.count = imagecursor.getCount();        
    this.thumbnails = new Bitmap[this.count];       
    for (int i = 0; i < this.count; i++) {


        int id = imagecursor.getInt(image_column_index);         

      thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(        
                getApplicationContext().getContentResolver(), id,       
                MediaStore.Images.Thumbnails.MICRO_KIND, null);     
      adapter.mThumbIds[i]= thumbnails[i];




Now the problem is that when I get count by imageCursor.getCount () it returns 0. So tell me what to do because I cannot fetch any media using this code.

Note. I am testing it on Galaxy Note-2


source to share

2 answers

Don't forget first

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


This method will return a list of photos in the gallery

public static ArrayList<String> getImagesPath(Activity activity) {
    Uri uri;
    ArrayList<String> listOfAllImages = new ArrayList<String>();
    Cursor cursor;
    int column_index_data, column_index_folder_name;
    String PathOfImage = null;
    uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    String[] projection = { MediaColumns.DATA,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME };

    cursor = activity.getContentResolver().query(uri, projection, null,
            null, null);

    column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
    column_index_folder_name = cursor
    while (cursor.moveToNext()) {
        PathOfImage = cursor.getString(column_index_data);

    return listOfAllImages;




just try it even if your device only has internal storage without external storage,

try adding permissions:

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


and read with





All Articles