Retrieve images of particular folder in Android
Asked Answered
S

4

7

In my application I created the GridView to show the image from particular folder. The issue is that I want to retrieve images from the DCIM/100ANDRO folder. Which type of arguments should be passed through query to retrieve such type of images? Please provide me solution.

I am using following code to retrieve which gives me images captured by camera

        //importing only camera images and storing in ArrayList of class Images     type
        String[] projection = {
        MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.Images.Media.DISPLAY_NAME
        };
        String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ?";
        String[] selectionArgs = new String[] {
            "Camera"
        };

        Cursor mImageCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null ); 

        if (mImageCursor != null)
        {

            mImageCursor.moveToFirst();

            for (int i = 0; i < mImageCursor.getCount(); i++)
            {
                Images im=new Images();
                eachImageView=new ImageView(this);
                int imageId = mImageCursor.getInt((mImageCursor.getColumnIndex( MediaStore.Images.Media._ID)));
                Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), 
                    imageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
                im.setBitmap(bm);
                eachImageView.setImageBitmap(bm);
                im.setImageView(eachImageView);

                arrayOfImages.add(im);

                mImageCursor.moveToNext();
            }
        }

Suggestion will be appreciated!

Studdingsail answered 13/2, 2013 at 16:51 Comment(4)
What is the code you're using right now ?Barker
I updated same now in the questionStuddingsail
Is it that you want to retrieve images from your sdcard? If that's the case you can simply use the sdcard path dynamicallyImpractical
There are some images in DCIM/100ANDRO folder and I want to retrieve that particular imagesStuddingsail
D
8
File folder = new File(Environment.getExternalStorageDirectory().toString() + "/Folder Name/");
folder.mkdirs();
File[] allFiles = folder.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".png"));
    }
});
Druse answered 30/1, 2016 at 12:26 Comment(0)
A
3

First, assert the path that you are accessing and then use Bitmap.DecodePath(path) to load the bitmap. Try this out.

File path = new File(Environment.getExternalStorageDirectory(),"DCIM/100ANDRO");
if(path.exists())
{
    String[] fileNames = path.list();
}
for(int i = 0; i < filename.length; i++)
{
     Bitmap mBitmap = Bitmap.decodeFile(path.getPath()+"/"+ fileNames[i]);
     ///Now set this bitmap on imageview
} 
Argal answered 13/2, 2013 at 17:45 Comment(1)
I got a deprecation warning, Environment.getExternalStorageDirectory. is it correct that is starting Kitkat and above?Vaclava
I
2

Once check this one

 File sdcardPath = new File(Environment.getExternalStorageDirectory()
    .getPath() + "/100ANDRO"); 
   Log.v(sdcardPath.getPath());

Check if this prints the correct path which you require If sdcardPath is not null then try the following logic

int imageCount = sdcardPath.listFiles().length;
  for (int count = 0; count < imageCount - 1; count++) {
   ImageView eachImageView= new ImageView(this);
   Bitmap bmp = BitmapFactory.decodeFile(sdcardPath.listFiles()[count].getAbsolutePath());
   eachImageView.setImageBitmap(bmp);
Impractical answered 13/2, 2013 at 17:16 Comment(1)
Generally i think dcim folder is the path of the sdcard. So i asked him to check if this gives him the correct path of the file or not. If this gives the correct path of the file i thought of getting the names of the files using listFiles()Impractical
C
0

If I understand you correctly, you wish to have your GridView show images from a certain folder on the device.

In your query, you use the following Uri: MediaStore.Images.Media.EXTERNAL_CONTENT_URI. This will search for images everywhere on the "primary" external storage. If you only want to search in a specific folder - use a specific Uri.

You can create a File object with the path of your location, then create a Uri from that File. For example:

File myFile = new File("/scard/myfolder");
Uri myUri = Uri.fromFile(myFile);

Then in your query for images, you can use this Uri instead. That is to say you would have:

Cursor mImageCursor = getContentResolver().query(myUri, projection, selection, selectionArgs, null );

Then you could still handle the returned cursor in the same way, as the format of data is the same. However, the returned data would only contain images from your desired folder.

Hope this helps :)

Cogswell answered 13/2, 2013 at 18:30 Comment(1)
I know this is an old post, but for anyone else who stumbles on this answer... it does not work. The Uri is required to be a Uri to a valid ContentProvider, NOT the folder you wish to grab media from.Stertor

© 2022 - 2024 — McMap. All rights reserved.