在Android中检索特定文件夹的图像

7
在我的应用程序中,我创建了GridView来显示特定文件夹中的图像。 问题是,我想从DCIM/100ANDRO文件夹检索图像。 应该通过查询传递哪种类型的参数以检索此类图像? 请为我提供解决方案。
我正在使用以下代码进行检索,它给出了由相机捕获的图像。
        //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();
            }
        }

欢迎提出意见建议!


1
你现在使用的代码是什么? - Mr.Me
我现在已经在问题中更新了相同的内容。 - void
你是想从SD卡中检索图像吗?如果是这样,你可以动态地使用SD卡路径。 - G_S
DCIM/100ANDRO文件夹中有一些图像,我想检索那些特定的图像。 - void
4个回答

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

3

首先,确认您要访问的路径,然后使用Bitmap.DecodePath(path)加载位图。试试这个。

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
} 

我收到了一个弃用警告,Environment.getExternalStorageDirectory。这是正确的,从Kitkat及以上开始吗? - Adi Prasetyo

2
请检查这个

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

检查是否打印出所需的正确路径 如果sdcardPath不为空,则尝试以下逻辑

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

一般来说,我认为DCIM文件夹是SD卡的路径。所以我让他检查这是否给出了正确的文件路径。 如果这给出了正确的文件路径,我想使用listFiles()获取文件名。 - G_S

0

如果我理解正确,您希望GridView从设备上的某个特定文件夹中显示图像。

在您的查询中,您使用以下Uri:MediaStore.Images.Media.EXTERNAL_CONTENT_URI。这将在“主”外部存储器上搜索所有图像。如果您只想在特定文件夹中搜索-请使用特定的Uri。

您可以使用位置路径创建一个File对象,然后从该文件创建一个Uri。例如:

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

然后在您的图像查询中,您可以使用这个Uri。也就是说,您会有:

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

然后您仍然可以以相同的方式处理返回的光标,因为数据的格式是相同的。但是,返回的数据将只包含您所需文件夹中的图像。

希望这能帮到您 :)


4
我知道这是一篇旧文章,但对于任何其他看到这个答案的人来说...它行不通。 Uri必须是指向有效ContentProvider的Uri,而不是您希望从中获取媒体的文件夹。 - NewGradDev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接