Skip to content

Commit

Permalink
#14: Added missing error-handling/message
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed May 21, 2019
1 parent 91ad004 commit ef6fba1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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);
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

<string name="toast_cannot_retrieve_selected_image">Beende app: Kein Quell Photo ausgewählt</string>
<string name="toast_saved_as">Gespeichert als \'%1$s\'</string>
<string name="toast_saved_error">Kann nicht speichern unter \'%1$s\'.\n\n%2$s</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

<string name="toast_cannot_retrieve_selected_image">Quitting: No source image selected</string>
<string name="toast_saved_as">Saved as \'%1$s\'</string>
<string name="toast_saved_error">Cannot save as \'%1$s\'.\n\n%2$s</string>
</resources>

0 comments on commit ef6fba1

Please sign in to comment.