[英]How do we get all folders containing images using MediaStore class (ANDROID)?
I have used MediaStore Class to get all the folders of images inside storage. 我已经使用MediaStore类来获取存储中所有图像的文件夹。 But the problem is it is showing the same folder as many times as the number of images in it. 但是问题在于它显示同一文件夹的次数是该文件夹中图像数量的多次。
public void getImageDirProvider(){
Uri uri= MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String projection[]={MediaStore.Images.Media.BUCKET_DISPLAY_NAME,MediaStore.Images.Media.BUCKET_ID,MediaStore.Images.Media._ID};
imageCursor=getActivity().getContentResolver().query(uri,projection,null,null,null);
if (imageCursor!=null){
while(imageCursor.moveToNext()){
bucketName.add(imageCursor.getString(0));
bucketId.add(imageCursor.getString(1));
}
}
}
Here bucketName and bucketId are ArrayLists of String type. 这里bucketName和bucketId是String类型的ArrayLists。
@Override
public int getCount() {
return bucketName.size();
}
@Override
public Object getItem(int position) {
return bucketName.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class Holder {
ImageView iview;
TextView tview;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder hold = new Holder();
convertView = layoutInflater.inflate(R.layout.my_list_adapter, parent, false);
hold.tview = (TextView) convertView.findViewById(R.id.text_view_adapter);
hold.iview = (ImageView) convertView.findViewById(R.id.image_view);
hold.tview.setText(getItem(position).toString());
hold.iview.setImageResource(android.R.drawable.ic_btn_speak_now);
return convertView;
}
}
Above is myCustomAdapter class extending BaseAdapter class. 上面是myCustomAdapter类,它扩展了BaseAdapter类。
String projection[]={MediaStore.Images.Media.BUCKET_DISPLAY_NAME,MediaStore.Images.Media.BUCKET_ID,MediaStore.Images.Media._ID};
you query for every available image hence returning cursor has as many records. 您查询每个可用图像,因此返回的游标具有尽可能多的记录。 So when you cycling over it you of course receive duplicate buckets. 因此,当您在上面骑车时,您当然会收到重复的存储桶。 Try to exclude images from query and query only for buckets available like 尝试从查询中排除图像,仅查询可用的存储桶,例如
String projection[]={MediaStore.Images.Media.BUCKET_DISPLAY_NAME,MediaStore.Images.Media.BUCKET_ID};
if you still recieve duplicate values then try String projection[] = {"DISTINCT " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.BUCKET_ID};
如果您仍然收到重复的值,请尝试使用String projection[] = {"DISTINCT " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.BUCKET_ID};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.