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

建议将不胜感激!

4

4 回答 4

7
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"));
    }
});
于 2016-01-30T12:26:46.967 回答
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
} 
于 2013-02-13T17:45:07.853 回答
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);
于 2013-02-13T17:16:50.160 回答
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 );

然后你仍然可以用同样的方式处理返回的游标,因为数据的格式是一样的。但是,返回的数据将仅包含您所需文件夹中的图像。

希望这可以帮助 :)

于 2013-02-13T18:30:41.270 回答