Skip to content

Commit 80c1281

Browse files
authored
Handle empty gallery cursor when selecting media (#3948)
1 parent cf1d1b3 commit 80c1281

File tree

1 file changed

+67
-45
lines changed

1 file changed

+67
-45
lines changed

Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8006,19 +8006,27 @@ private String getImageFilePath(Uri uri) {
80068006
//String[] filePaths = file.getPath().split(":");
80078007
//String image_id = filePath[filePath.length - 1];
80088008
String[] filePathColumn = {MediaStore.Images.Media.DATA};
8009-
Cursor cursor = getContext().getContentResolver().query(
8010-
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
8011-
new String[]{ MediaStore.Images.Media.DATA},
8012-
null,
8013-
null,
8014-
null
8015-
);
8016-
cursor.moveToFirst();
8017-
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
8018-
String filePath = cursor.getString(columnIndex);
8019-
cursor.close();
8020-
8021-
if (filePath == null || "content".equals(scheme)) {
8009+
Cursor cursor = getContext().getContentResolver().query(
8010+
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
8011+
new String[]{ MediaStore.Images.Media.DATA},
8012+
null,
8013+
null,
8014+
null
8015+
);
8016+
// Some gallery providers may return an empty cursor on modern Android builds.
8017+
String filePath = null;
8018+
if (cursor != null) {
8019+
try {
8020+
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
8021+
if (columnIndex >= 0 && cursor.moveToFirst()) {
8022+
filePath = cursor.getString(columnIndex);
8023+
}
8024+
} finally {
8025+
cursor.close();
8026+
}
8027+
}
8028+
8029+
if (filePath == null || "content".equals(scheme)) {
80228030
//if the file is not on the filesystem download it and save it
80238031
//locally
80248032
try {
@@ -8159,24 +8167,26 @@ else if (requestCode == FILECHOOSER_RESULTCODE) {
81598167
Uri selectedImage = intent.getData();
81608168
String scheme = intent.getScheme();
81618169

8162-
String[] filePathColumn = {MediaStore.Images.Media.DATA};
8163-
Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
8164-
8165-
// this happens on Android devices, not exactly sure what the use case is
8166-
if(cursor == null) {
8167-
callback.fireActionEvent(null);
8168-
return;
8169-
}
8170-
8171-
cursor.moveToFirst();
8172-
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
8173-
String filePath = cursor.getString(columnIndex);
8174-
cursor.close();
8175-
boolean fileExists = false;
8176-
if (filePath != null) {
8177-
File file = new File(filePath);
8178-
fileExists = file.exists() && file.canRead();
8179-
}
8170+
String[] filePathColumn = {MediaStore.Images.Media.DATA};
8171+
Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
8172+
8173+
// Some gallery providers may return an empty cursor on modern Android builds.
8174+
String filePath = null;
8175+
if (cursor != null) {
8176+
try {
8177+
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
8178+
if (columnIndex >= 0 && cursor.moveToFirst()) {
8179+
filePath = cursor.getString(columnIndex);
8180+
}
8181+
} finally {
8182+
cursor.close();
8183+
}
8184+
}
8185+
boolean fileExists = false;
8186+
if (filePath != null) {
8187+
File file = new File(filePath);
8188+
fileExists = file.exists() && file.canRead();
8189+
}
81808190

81818191
if (!fileExists && "content".equals(scheme)) {
81828192
//if the file is not on the filesystem download it and save it
@@ -8204,26 +8214,33 @@ else if (requestCode == FILECHOOSER_RESULTCODE) {
82048214
}
82058215
}
82068216

8207-
callback.fireActionEvent(new ActionEvent(new String[]{filePath}));
8208-
return;
8217+
if (filePath == null) {
8218+
callback.fireActionEvent(null);
8219+
return;
8220+
}
8221+
8222+
callback.fireActionEvent(new ActionEvent(new String[]{filePath}));
8223+
return;
82098224
} else if (requestCode == OPEN_GALLERY) {
82108225

82118226
Uri selectedImage = intent.getData();
82128227
String scheme = intent.getScheme();
82138228

8214-
String[] filePathColumn = {MediaStore.Images.Media.DATA};
8215-
Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
8216-
8217-
// this happens on Android devices, not exactly sure what the use case is
8218-
if(cursor == null) {
8219-
callback.fireActionEvent(null);
8220-
return;
8229+
String[] filePathColumn = {MediaStore.Images.Media.DATA};
8230+
Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
8231+
8232+
// Some gallery providers may return an empty cursor on modern Android builds.
8233+
String filePath = null;
8234+
if (cursor != null) {
8235+
try {
8236+
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
8237+
if (columnIndex >= 0 && cursor.moveToFirst()) {
8238+
filePath = cursor.getString(columnIndex);
8239+
}
8240+
} finally {
8241+
cursor.close();
8242+
}
82218243
}
8222-
8223-
cursor.moveToFirst();
8224-
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
8225-
String filePath = cursor.getString(columnIndex);
8226-
cursor.close();
82278244
boolean fileExists = false;
82288245
if (filePath != null) {
82298246
File file = new File(filePath);
@@ -8256,6 +8273,11 @@ else if (requestCode == FILECHOOSER_RESULTCODE) {
82568273
}
82578274
}
82588275

8276+
if (filePath == null) {
8277+
callback.fireActionEvent(null);
8278+
return;
8279+
}
8280+
82598281
callback.fireActionEvent(new ActionEvent(filePath));
82608282
return;
82618283
} else {

0 commit comments

Comments
 (0)