Retrieve images of particular folder in Android

南楼画角 提交于 2019-11-29 04:51:41
praveen s
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"));
    }
});

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
} 

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);

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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!