I know this question has been asked before, but I've gone though all the posts I could find and no solution helped.
I'm just trying to find the pictures in storage (they are not broken, as Google Photos sees them all and the Files app also shows all the folder with images, so I assume they are also properly indexed).
Here is the Kotlin code I'm using:
val cursor1: Cursor? =
applicationContext.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
arrayOf(
MediaStore.Images.Media._ID
), null, null,
MediaStore.Images.Media.DEFAULT_SORT_ORDER
)
if (cursor1 != null) {
Log.i("Files", "passed query, cursor is size ${cursor1.count}")
}
val actualImageColumnIndex: Int? =
cursor1?.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
if (cursor1 != null) {
Log.i("Files", "cursor is not null")
while (cursor1.moveToNext()) {
// val i: String = cursor.getString(actualImageColumnIndex)
val i: String? =
actualImageColumnIndex?.let { cursor1.getString(it) }
Log.i("Files", "file found: $i.")
}
} else
Log.i("Files", "cursor is null")
cursor1?.close()
I also tried to do the same thing from Java:
public class Photo {
public static ArrayList<Photo> getAllImages(Cursor cursor, Activity activity) {
int i = 0;
ArrayList<Photo> arrayList = new ArrayList<>();
if (cursor == null) {
ContentResolver resolver = activity.getContentResolver();
cursor = resolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
null,
null,
null);
if (cursor != null) {
while (i < cursor.getCount()) {
cursor.moveToPosition(i);
int fieldIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
Long id = cursor.getLong(fieldIndex);
Uri imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
Photo photo = new Photo();
photo.setName("");
photo.setUri(imageUri);
arrayList.add(photo);
i++;
}
cursor.close();
}
}
return arrayList;
}
private String name;
private Uri uri;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Uri getUri() {
return uri;
}
public Photo(String name, Uri uri) {
this.name = name;
this.uri = uri;
}
public Photo() {
}
public void setUri(Uri uri) {
this.uri = uri;
}
}
, and I call the static function like this:
val cursorJava: ArrayList<Photo> = Photo.getAllImages(null, this)
Log.i("Files", "files found with java: ${cursorJava.size}.")
All of this is done in the onCreate method of MainActivity. The rest of the application (a button opening a new activity displying some photos from the web in a gridView) works well.
I already tried:
- giving READ_EXTERNAL_STORAGE from the manifest
- serching using INTERNAL_CONTENT_URI instead of EXTERNAL (though, at least to my understanding, EXTERNAL is the one to use)
- in build.gradle:
targetSdkVersion/compileSdkVersion 30/29and running the app on VMs with Android 11/10, + USB debugging on Android 10 - using
android:requestLegacyExternalStorage="true"for Android 10
I apologize in advance if it's some stupid mistake, but I've been sitting here for hours and made no progress.