Skip to content

Commit

Permalink
Add catch for downloaded image crash (#73)
Browse files Browse the repository at this point in the history
* Add catch for crash
* Clean up code
  • Loading branch information
nkalupahana committed Oct 30, 2023
1 parent bbe8a33 commit 9da134a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Expand Up @@ -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());
Expand Down Expand Up @@ -304,6 +309,7 @@ private void _saveMedia(PluginCall call) {
call.resolve(result);
} catch (RuntimeException e) {
call.reject("RuntimeException occurred", e);
return;
}
}

Expand Down
1 change: 0 additions & 1 deletion example/capacitor.config.ts
Expand Up @@ -4,7 +4,6 @@ const config: CapacitorConfig = {
appId: 'io.capacitorcommunity.media',
appName: 'Capacitor Community Media Example',
webDir: 'build',
bundledWebRuntime: false
};

export default config;
2 changes: 1 addition & 1 deletion example/src/components/GetMedias.tsx
Expand Up @@ -45,7 +45,7 @@ const GetMedias = () => {
<IonButton onClick={getMediasFavorites}>Get 9 Favorites</IonButton>
<IonButton onClick={getMediasSortedFavorites}>Get 9 Images Last Created in Favorites</IonButton>
<br />
{ medias?.map(media => <img style={{"width": "50px"}} src={"data:image/jpeg;base64," + media.data} />) }
{ medias?.map(media => <img key={media.identifier} alt="Result Image" style={{"width": "50px"}} src={"data:image/jpeg;base64," + media.data} />) }
</>
};

Expand Down

0 comments on commit 9da134a

Please sign in to comment.