From 21686a6a934727a73e928b29fb64c5478d9ab119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 7 Oct 2021 21:07:35 +0200 Subject: [PATCH] Android: Catch some exceptions --- .../src/org/ppsspp/ppsspp/NativeActivity.java | 51 ++++++++++++------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/android/src/org/ppsspp/ppsspp/NativeActivity.java b/android/src/org/ppsspp/ppsspp/NativeActivity.java index b62af3fbc3..fe7cba3de8 100644 --- a/android/src/org/ppsspp/ppsspp/NativeActivity.java +++ b/android/src/org/ppsspp/ppsspp/NativeActivity.java @@ -1118,30 +1118,39 @@ public abstract class NativeActivity extends Activity { return; } if (requestCode == RESULT_LOAD_IMAGE) { - Uri selectedImage = data.getData(); - if (selectedImage != null) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - NativeApp.sendMessage("bgImage_updated", selectedImage.toString()); - } else { - String[] filePathColumn = {MediaStore.Images.Media.DATA}; - Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); - if (cursor != null) { - cursor.moveToFirst(); - int columnIndex = cursor.getColumnIndex(filePathColumn[0]); - String picturePath = cursor.getString(columnIndex); - cursor.close(); - - NativeApp.sendMessage("bgImage_updated", picturePath); + try { + Uri selectedImage = data.getData(); + if (selectedImage != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + NativeApp.sendMessage("bgImage_updated", selectedImage.toString()); + } else { + String[] filePathColumn = {MediaStore.Images.Media.DATA}; + Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); + if (cursor != null) { + cursor.moveToFirst(); + int columnIndex = cursor.getColumnIndex(filePathColumn[0]); + String picturePath = cursor.getString(columnIndex); + cursor.close(); + NativeApp.sendMessage("bgImage_updated", picturePath); + } } } + } catch (Exception e) { + Log.w(TAG, "Exception receiving image: " + e.toString()); } } else if (requestCode == RESULT_OPEN_DOCUMENT) { Uri selectedFile = data.getData(); if (selectedFile != null) { - // Grab permanent permission so we can show it in recents list etc. - if (Build.VERSION.SDK_INT >= 19) { - getContentResolver().takePersistableUriPermission(selectedFile, Intent.FLAG_GRANT_READ_URI_PERMISSION); + try { + // Grab permanent permission so we can show it in recents list etc. + if (Build.VERSION.SDK_INT >= 19) { + getContentResolver().takePersistableUriPermission(selectedFile, Intent.FLAG_GRANT_READ_URI_PERMISSION); + } + } catch (Exception e) { + Log.w(TAG, "Exception getting permissions for document: " + e.toString()); } + // Even if we got an exception getting permissions, try to pass along the file. Maybe this version of Android + // doesn't need it. Log.i(TAG, "Browse file finished:" + selectedFile.toString()); NativeApp.sendMessage("browse_fileSelect", selectedFile.toString()); } @@ -1150,7 +1159,13 @@ public abstract class NativeActivity extends Activity { if (selectedDirectoryUri != null) { String path = selectedDirectoryUri.toString(); Log.i(TAG, "Browse folder finished: " + path); - getContentResolver().takePersistableUriPermission(selectedDirectoryUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + try { + getContentResolver().takePersistableUriPermission(selectedDirectoryUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + } catch (Exception e) { + Log.w(TAG, "Exception getting permissions for document: " + e.toString()); + } + // Even if we got an exception getting permissions, try to pass along the file. Maybe this version of Android + // doesn't need it. If we can't access it, we'll fail in some other way later. DocumentFile documentFile = DocumentFile.fromTreeUri(this, selectedDirectoryUri); Log.i(TAG, "Document name: " + documentFile.getUri()); /*