Итак, я загружаю все изображения из галереи, она работает нормально, но 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);
}
});
private ArrayList<String> getGalleryImagesPath() {
ArrayList<String> imagePathList = new ArrayList<String>();
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
// MediaStore.MediaColumns.DATA is deprecated
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
imagePathList.add(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)));
}
cursor.close();
}
return imagePathList;
}
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;
}
}
Обновление # 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;
}