使用Android Studio [Java]中的内容解析器从图库中检索所有图像

时间:2016-03-17 21:57:49

标签: java android android-studio

我正在尝试从图像厨房中抓取所有图像并将其显示在网格视图中。我被困住了,不知道从哪里开始。如果我运行此代码,则游标返回getCount为0.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    String[] imageStrings = {MediaStore.Images.Thumbnails._ID};

    Uri uri = MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI;

    Cursor cursor = getActivity().getContentResolver().query(uri, imageStrings, null,
        null, MediaStore.Images.Thumbnails.IMAGE_ID);


    Log.d("onActivityCreated: ", cursor.getCount()+"");

}

1 个答案:

答案 0 :(得分:2)

  

如果我运行此代码,则游标返回的getCount为0。

尝试将MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI更改为MediaStore.Images.MEDIA.EXTERNAL_CONTENT_URI

修改了代码

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            String[] imageStrings = {MediaStore.Images.Thumbnails._ID};

            Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;

            Cursor cursor = getActivity().getContentResolver().query(uri, imageStrings, null, 
                        null, MediaStore.Images.Thumbnails.IMAGE_ID);


           Log.d("onActivityCreated: ", cursor.getCount()+"");

    }

有关详情,请查看@Tarun answer

希望这会有所帮助!!