From ef6fba1e03a2f1e1988b1eae489fa8ccd72ea3d0 Mon Sep 17 00:00:00 2001 From: k3b <1374583+k3b@users.noreply.github.com> Date: Tue, 21 May 2019 12:32:57 +0200 Subject: [PATCH] #14: Added missing error-handling/message --- .../CropAreasChooseBaseActivity.java | 8 ++++++- .../CropAreasEditActivity.java | 24 ++++++++++++++++++- .../lossless_jpg_crop/ImageProcessor.java | 7 +++--- app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/de/k3b/android/lossless_jpg_crop/CropAreasChooseBaseActivity.java b/app/src/main/java/de/k3b/android/lossless_jpg_crop/CropAreasChooseBaseActivity.java index c0b19b3..3d73a91 100644 --- a/app/src/main/java/de/k3b/android/lossless_jpg_crop/CropAreasChooseBaseActivity.java +++ b/app/src/main/java/de/k3b/android/lossless_jpg_crop/CropAreasChooseBaseActivity.java @@ -225,7 +225,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); } - protected void crop(InputStream inStream, OutputStream outStream, Rect rect) { + protected void crop(InputStream inStream, OutputStream outStream, Rect rect) throws IOException { this.mSpectrum.crop(inStream, outStream, rect, 0); } @@ -276,7 +276,13 @@ protected Uri cropToSharedUri() { outUri = FileProvider.getUriForFile(this, "de.k3b.llCrop", outFile); } catch (Exception e) { + // #14: delete affected file as it is useless + close(outStream, outStream); + outFile.delete(); Log.e(TAG, "Error " + context_message + "(" + outUri +") => " + e.getMessage(), e); + Toast.makeText(this, + getString(R.string.toast_saved_error, outFile.getAbsolutePath(), e.getMessage()), + Toast.LENGTH_LONG).show(); } finally { close(outStream, outStream); close(inStream, inStream); diff --git a/app/src/main/java/de/k3b/android/lossless_jpg_crop/CropAreasEditActivity.java b/app/src/main/java/de/k3b/android/lossless_jpg_crop/CropAreasEditActivity.java index 19d35b6..62715fb 100644 --- a/app/src/main/java/de/k3b/android/lossless_jpg_crop/CropAreasEditActivity.java +++ b/app/src/main/java/de/k3b/android/lossless_jpg_crop/CropAreasEditActivity.java @@ -7,6 +7,7 @@ import android.net.Uri; import android.os.Bundle; import android.provider.DocumentsContract; +import android.provider.DocumentsProvider; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -135,7 +136,13 @@ private boolean openPublicOutputUriPicker(int folderpickerCode) { return true; } - private void onOpenPublicOutputUriPickerResult(Uri outUri) { + private void onOpenPublicOutputUriPickerResult(Uri _outUri) { + + // use to provoke an error to test error handling + // Uri outUri = Uri.parse(_outUri + "-err"); + + Uri outUri = _outUri; + final Intent intent = getIntent(); final Uri inUri = getSourceImageUri(getIntent()); @@ -161,7 +168,22 @@ private void onOpenPublicOutputUriPickerResult(Uri outUri) { finish(); return; } catch (Exception e) { + close(outStream, outStream); + Log.e(TAG, "Error " + context_message + e.getMessage(), e); + + try { + // #14: delete affected file as it is useless + DocumentsContract.deleteDocument(getContentResolver(), _outUri); + } catch (Exception exDelete) { + // ignore if useless file cannot be deleted + } + + Log.e(TAG, "Error " + context_message + "(" + outUri +") => " + e.getMessage(), e); + Toast.makeText(this, + getString(R.string.toast_saved_error, toString(outUri), e.getMessage()), + Toast.LENGTH_LONG).show(); + } finally { close(outStream, outStream); close(inStream, inStream); diff --git a/app/src/main/java/de/k3b/android/lossless_jpg_crop/ImageProcessor.java b/app/src/main/java/de/k3b/android/lossless_jpg_crop/ImageProcessor.java index 816f91b..271cdc8 100644 --- a/app/src/main/java/de/k3b/android/lossless_jpg_crop/ImageProcessor.java +++ b/app/src/main/java/de/k3b/android/lossless_jpg_crop/ImageProcessor.java @@ -16,6 +16,7 @@ import com.facebook.spectrum.plugins.SpectrumPluginJpeg; import com.facebook.spectrum.requirements.EncodeRequirement; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -34,11 +35,11 @@ public ImageProcessor() { // DefaultPlugins.get()); // JPEG, PNG and WebP plugins } - public void crop(InputStream inputStream, OutputStream outputStream, Rect rect, int degrees) { + public void crop(InputStream inputStream, OutputStream outputStream, Rect rect, int degrees) throws IOException { crop(inputStream, outputStream, rect.left, rect.top, rect.right, rect.bottom, degrees); } - public void crop(InputStream inputStream, OutputStream outputStream, int left, int top, int right, int bottom, int degrees) { + public void crop(InputStream inputStream, OutputStream outputStream, int left, int top, int right, int bottom, int degrees) throws IOException { final EncodeRequirement encoding = new EncodeRequirement(EncodedImageFormat.JPEG, 80, EncodeRequirement.Mode.LOSSLESS); try { @@ -54,7 +55,7 @@ public void crop(InputStream inputStream, OutputStream outputStream, int left, i .build(), "my_callsite_identifier"); } catch (Exception ex) { - throw new RuntimeException("Cannot Transcode from " + inputStream + " to " + outputStream + " : " + ex.getMessage(), ex); + throw new IOException("Cannot Transcode from " + inputStream + " to " + outputStream + " : " + ex.getMessage(), ex); } } } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 0ec7464..c3cdf11 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -11,4 +11,5 @@ Beende app: Kein Quell Photo ausgewählt Gespeichert als \'%1$s\' + Kann nicht speichern unter \'%1$s\'.\n\n%2$s diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 85b036c..47f0d88 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -11,4 +11,5 @@ Quitting: No source image selected Saved as \'%1$s\' + Cannot save as \'%1$s\'.\n\n%2$s