Android Display Images From Specific Folder in Gallery View
Asked Answered
A

1

6

I'm trying to make a little app that displays only images from a specific folder in a gallery view. I have found a few examples, but every single one just ends up displaying only 1 image. This example I will post below was a WONDERFUL help, it does almost exactly what I want it to do, I just need to change it to display images from the specific folder, and not all folders. I've given this a few days worth of attempts, but I just don't seem to be adding in the right code. I feel like its a very simple thing that I am missing too. Any help would be greatly appreciated!

public class AndroidCustomGallery extends Activity {

    private int count;
    private Bitmap[] thumbnails;
    private boolean[] thumbnailsselection;
    private String[] arrPath;
    private ImageAdapter imageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };

                final String orderBy = MediaStore.Images.Media._ID;

                Cursor  imagecursor = getContentResolver().query(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,        
                        null, orderBy);




                int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);          
                this.count = imagecursor.getCount();        
                this.thumbnails = new Bitmap[this.count];       
                this.arrPath = new String[this.count];  
                this.thumbnailsselection = new boolean[this.count];

                for (int i = 0; i < this.count; i++) {

                    imagecursor.moveToPosition(i);

                    int id = imagecursor.getInt(image_column_index);        
                    int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA); 

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

                    arrPath[i]= imagecursor.getString(dataColumnIndex);

                }

                GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);      
                imageAdapter = new ImageAdapter();      
                imagegrid.setAdapter(imageAdapter);     
                imagecursor.close();
Airway answered 7/1, 2013 at 16:47 Comment(1)
what is the content of your ImageAdapter classProfiterole
A
14

Figured it out! Below is the pasted code for anyone who wants to do something similar.

Cursor imagecursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        columns, 
                        MediaStore.Images.Media.DATA + " like ? ",
                        new String[] {"%/yourfoldername/%"},  
                        null);
Airway answered 7/1, 2013 at 22:29 Comment(2)
Glad to be of assistance :)Airway
How do we specify the folder name exactly? For eg: "Images" folder. Is it: /mnt/sdcard/Images or String dirName = Environment.getExternalStorageDirectory() + "/Images". For the 4th argument what do we specify? new String[] {"/mnt/sdcard/Images"} or new String[] {"Images"} or simply dirName?Physical

© 2022 - 2024 — McMap. All rights reserved.