From 9da134ac8324e59c33d3190cab830348b44e5c1b Mon Sep 17 00:00:00 2001 From: Nisala Kalupahana Date: Mon, 30 Oct 2023 12:36:51 -0500 Subject: [PATCH] Add catch for downloaded image crash (#73) * Add catch for crash * Clean up code --- .../community/media/MediaPlugin.java | 18 ++++++++++++------ example/capacitor.config.ts | 1 - example/src/components/GetMedias.tsx | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/android/src/main/java/com/getcapacitor/community/media/MediaPlugin.java b/android/src/main/java/com/getcapacitor/community/media/MediaPlugin.java index 770f7b7..0997e9d 100644 --- a/android/src/main/java/com/getcapacitor/community/media/MediaPlugin.java +++ b/android/src/main/java/com/getcapacitor/community/media/MediaPlugin.java @@ -263,12 +263,17 @@ private void _saveMedia(PluginCall call) { final Cursor cursor = manager.query(new DownloadManager.Query().setFilterById(requestID)); cursor.moveToFirst(); inputPath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); - Uri downloadsUri = Uri.parse(inputPath); - File fileInDownloads = new File(downloadsUri.getPath()); - String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(new Date()); - inputFile = copyFile(fileInDownloads, getContext().getCacheDir(), "IMG_" + timeStamp); - fileInDownloads.delete(); - cursor.close(); + try { + Uri downloadsUri = Uri.parse(inputPath); + File fileInDownloads = new File(downloadsUri.getPath()); + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(new Date()); + inputFile = copyFile(fileInDownloads, getContext().getCacheDir(), "IMG_" + timeStamp); + fileInDownloads.delete(); + cursor.close(); + } catch (RuntimeException e) { + call.reject("RuntimeException occurred", e); + return; + } } else { Uri inputUri = Uri.parse(inputPath); inputFile = new File(inputUri.getPath()); @@ -304,6 +309,7 @@ private void _saveMedia(PluginCall call) { call.resolve(result); } catch (RuntimeException e) { call.reject("RuntimeException occurred", e); + return; } } diff --git a/example/capacitor.config.ts b/example/capacitor.config.ts index 7b43a30..efc0678 100644 --- a/example/capacitor.config.ts +++ b/example/capacitor.config.ts @@ -4,7 +4,6 @@ const config: CapacitorConfig = { appId: 'io.capacitorcommunity.media', appName: 'Capacitor Community Media Example', webDir: 'build', - bundledWebRuntime: false }; export default config; diff --git a/example/src/components/GetMedias.tsx b/example/src/components/GetMedias.tsx index 7c0ae92..de43649 100644 --- a/example/src/components/GetMedias.tsx +++ b/example/src/components/GetMedias.tsx @@ -45,7 +45,7 @@ const GetMedias = () => { Get 9 Favorites Get 9 Images Last Created in Favorites
- { medias?.map(media => ) } + { medias?.map(media => Result Image) } };