Skip to content

Commit

Permalink
feat(camera): Support for 1 Gallery app (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Jan 28, 2022
1 parent 8b6f119 commit 77e8c97
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -493,23 +493,37 @@ private void processEditedImage(PluginCall call, ActivityResult result) {
private Uri saveImage(Uri uri, InputStream is) throws IOException {
File outFile = null;
if (uri.getScheme().equals("content")) {
String filename = Uri.parse(Uri.decode(uri.toString())).getLastPathSegment();
if (!filename.contains(".jpg") && !filename.contains(".jpeg")) {
filename += "." + (new java.util.Date()).getTime() + ".jpeg";
}
File cacheDir = getContext().getCacheDir();
outFile = new File(cacheDir, filename);
outFile = getTempFile(uri);
} else {
outFile = new File(uri.getPath());
}
try {
writePhoto(outFile, is);
} catch (FileNotFoundException ex) {
// Some gallery apps return read only file url, create a temporary file for modifications
outFile = getTempFile(uri);
writePhoto(outFile, is);
}
return Uri.fromFile(outFile);
}

private void writePhoto(File outFile, InputStream is) throws IOException {
FileOutputStream fos = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
return Uri.fromFile(outFile);
}

private File getTempFile(Uri uri) {
String filename = Uri.parse(Uri.decode(uri.toString())).getLastPathSegment();
if (!filename.contains(".jpg") && !filename.contains(".jpeg")) {
filename += "." + (new java.util.Date()).getTime() + ".jpeg";
}
File cacheDir = getContext().getCacheDir();
return new File(cacheDir, filename);
}

/**
Expand Down

0 comments on commit 77e8c97

Please sign in to comment.