Android 如何从API 29中的图库中获取所有图像?

Android 如何从API 29中的图库中获取所有图像?,android,android-10.0,Android,Android 10.0,因此,我正在从库中加载所有图像,它工作正常,但不推荐使用MediaStore.MediaColumns.DATA。我找不到使用最新方法执行此任务而没有MediaStore.MediaColumns.DATA 我现在的代码 imageGallery = findViewById(R.id.image_grid); ArrayList<String> imagePathList = getGalleryImagesPath(); final GridA

因此,我正在从库中加载所有图像,它工作正常,但不推荐使用
MediaStore.MediaColumns.DATA
。我找不到使用最新方法执行此任务而没有
MediaStore.MediaColumns.DATA

我现在的代码

imageGallery = findViewById(R.id.image_grid);

        ArrayList<String> imagePathList = getGalleryImagesPath();

        final GridAdapter gridAdapter = new GridAdapter(AddPostActivity.this, imagePathList);

        imageGallery.setAdapter(gridAdapter);

        imageGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Picasso.get().load(new File(gridAdapter.getItem(position))).resizeDimen(R.dimen.add_post_image, R.dimen.add_post_image).into(addPostImage);
                postButton.setEnabled(true);
            }
        });

imageGallery=findviewbyd(R.id.image\u网格);
ArrayList imagePathList=getGalleryImagesPath();
final GridAdapter GridAdapter=新GridAdapter(AddPostActivity.this,imagePathList);
设置适配器(gridAdapter);
imageGallery.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共虚线单击(AdapterView AdapterView,视图视图,内部位置,长l){
Picasso.get().load(新文件(gridAdapter.getItem(position)).resizeDimen(R.dimen.add_post_image,R.dimen.add_post_image).into(addPostImage);
postButton.setEnabled(真);
}
});
private ArrayList getGalleryImagesPath(){
ArrayList imagePathList=新建ArrayList();
Uri Uri=MediaStore.Images.Media.EXTERNAL\u CONTENT\u Uri;
//MediaStore.MediaColumns.DATA已弃用
字符串[]投影={MediaStore.MediaColumns.DATA};
Cursor Cursor=getContentResolver().query(uri、投影、null、null、null);
如果(光标!=null){
while(cursor.moveToNext()){
add(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
}
cursor.close();
}
返回imagePathList;
}
私有静态类GridAdapter扩展BaseAdapter{
私有最终ArrayList imagePathList;
私人最终语境;
GridAdapter(上下文上下文,ArrayList imagePathList){
this.imagePathList=imagePathList;
this.context=上下文;
}
@凌驾
public int getCount(){
返回imagePathList.size();
}
@凌驾
公共字符串getItem(int位置){
返回imagePathList.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组视图组){
SquareImageView网格图像;
if(convertView==null){
gridImage=新的SquareImageView(上下文);
}否则{
gridImage=(SquareImageView)convertView;
}
Picasso.get().load(新文件(getItem(position))).resize(200200).into(gridImage);
返回网格图像;
}
}
更新#1

private ArrayList<Uri> getGalleryImagesPath() {

        ArrayList<Uri> imagePathList = new ArrayList<Uri>();

        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        String[] projection = {MediaStore.MediaColumns._ID};

        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
                imagePathList.add(ContentUris.withAppendedId(uri, id));
            }
            cursor.close();
        }

        return imagePathList;
    }
private ArrayList getGalleryImagesPath(){
ArrayList imagePathList=新建ArrayList();
Uri Uri=MediaStore.Images.Media.EXTERNAL\u CONTENT\u Uri;
字符串[]投影={MediaStore.MediaColumns.\u ID};
Cursor Cursor=getContentResolver().query(uri、投影、null、null、null);
如果(光标!=null){
while(cursor.moveToNext()){
int id=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._id));
add(ContentUris.withAppendedId(uri,id));
}
cursor.close();
}
返回imagePathList;
}

Have
getGalleryImagesPath()
返回一个
ArrayList
。查询
MediaStore
以检索图像的
\u ID
。使用
ContentUris.withAppendedId()
根据查询中的
Uri
MediaStore.Images.Media.EXTERNAL\u CONTENT\u Uri
在当前代码中)和给定图像的
\u ID
构建
Uri
。使用它填充返回的
ArrayList
并将这些
Uri
值用于毕加索。请参阅使用
ContentUris.withAppendedId()
和毕加索的Java示例。@Commonware我添加了一个更新,您可以看看它是否合适吗?即兴,看起来不错。然后,毕加索(或Glide)可以使用
Uri
来填充
ImageView
。在Android 10+上,您需要按住
读取外部存储
,才能访问其他应用程序的图像——我将在中介绍更多内容。
    private static class GridAdapter extends BaseAdapter {

        private final ArrayList<String> imagePathList;
        private final Context context;

        GridAdapter(Context context, ArrayList<String> imagePathList) {
            this.imagePathList = imagePathList;
            this.context = context;
        }

        @Override
        public int getCount() {
            return imagePathList.size();
        }

        @Override
        public String getItem(int position) {
            return imagePathList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {

            SquareImageView gridImage;

            if (convertView == null) {
                gridImage = new SquareImageView(context);

            } else {
                gridImage = (SquareImageView) convertView;
            }

            Picasso.get().load(new File(getItem(position))).resize(200, 200).into(gridImage);

            return gridImage;
        }
    }

private ArrayList<Uri> getGalleryImagesPath() {

        ArrayList<Uri> imagePathList = new ArrayList<Uri>();

        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        String[] projection = {MediaStore.MediaColumns._ID};

        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
                imagePathList.add(ContentUris.withAppendedId(uri, id));
            }
            cursor.close();
        }

        return imagePathList;
    }