Search code examples
androidlocal-storagestorageandroid-gallerymediastore

Android mediastore not returning all internal storage images in phone. It is only showing images lockscreen and wallpaper images. Any solution?


Please give me a solution for loading all images from internal and external storage of android phone? My method below returns only wallpapers and lockscreen images. For testing, I'm using mi redmi note 3 with android v6.0 os and No External storage inserted. Please give me the latest solution. I'm a beginner to android.

public ArrayList<String> getImagePaths() {
            ArrayList<String> resultIAV = new ArrayList<String>();
            URI u = MediaStore.Images.Media.INTERNAL_CONTENT_URI;

            Cursor c = getContentResolver().query(u, projection, null,
                    null, null);

            column_index_data = c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);

            while (c.moveToNext()) {
                absolutePathOfImage = c.getString(column_index_data);
                resultIAV.add(absolutePathOfImage);
            }

            return resultIAV;
    }

Solution

  • Use this to get all images from internal and External storage and do not forget to add
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> permission in your manifest file.

    int dataColumnIndex;
    int bucketColumnIndex;
    int displayNameColumnIndex;
    String GalleryThumbnail_Path;
    String bucket;
    String displayName;
    int imageCounter = 0;
    
    Thread getImages = new Thread(new Runnable() {
        @Override
        public void run() {
            String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_TAKEN,
                    MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.BUCKET_DISPLAY_NAME
            };
            String orderBy = MediaStore.Images.Media.DATE_TAKEN;
            Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            try {
                Cursor cursor = context.getContentResolver().query(uri, columns, null, null, orderBy);
                assert cursor != null;
                int mCount = cursor.getCount();
                for (int i = mCount - 1; i >= 0; i--) {
                    if (imageCounter <= 370) {
                        cursor.moveToPosition(i);
                        dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                        bucketColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
                        displayNameColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
                        GalleryThumbnail_Path = cursor.getString(dataColumnIndex);
                        bucket = cursor.getString(bucketColumnIndex);
                        displayName = cursor.getString(displayNameColumnIndex);
                        myimagesPath.add(GalleryThumbnail_Path);
                        myStorage.add(bucket + "-->" + displayName);
                        imageCounter++;
                        Log.e("Recents " + i + "-->>", "Added to bucket");
                    } else {
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    

    This works in Android 7.1.2 also (My OnePlus One-which i use for development)