简体   繁体   English

我们如何使用MediaStore类(ANDROID)获取所有包含图像的文件夹?

[英]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.

相关问题 如何使用 mediastore 获取所有音频文件 - How to get all the audio file using mediastore 如何获取android中我的图库中的图像的MediaStore.Images.ImageColumns.TITLE? - How to get MediaStore.Images.ImageColumns.TITLE of an image in my gallery in android? 如何使用MediaStore获取特定目录和所有子目录中所有音乐的列表 - How to get list of all music in specific directory and all subdirectories using MediaStore 如何使用MediaStore保存Android Q中的图片? - How to save an image in Android Q using MediaStore? 如何在 android 工作室中使用 MediaStore Api 在 android 11 中获取特定文件夹,如 WhatsApp (.Status) 文件夹 - How to get specific folder like WhatsApp (.Status) folder in android 11 using MediaStore Api in android studio 如何使用 MediaStore 从具有路径、持续时间和缩略图的特定文件夹中获取所有视频? - How to get all video from specific folder with path,duration and thumbnail using MediaStore? 如何设置 MEDIASTORE 以从 android 上的特定文件夹获取图像 - how to set MEDIASTORE to get image from specific folder on android 如何列出android上的所有文件夹? - How to list all folders on android? 如何使用Mockito测试api? 我们需要模拟所有的类变量吗? - How to test api using Mockito? Do we need to mock all the class variables? 如何使用 MediaStore API 访问 Android 11 的 Whatsapp 文件夹? - How to access Whatsapp folder of Android 11 by using MediaStore API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM