Skip to content

Commit

Permalink
Added error handling and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Apr 24, 2019
1 parent 37edee3 commit e4d331e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

// all supported locales. Note: the lib has more translations which are supressed here
// resConfigs "ar","de","es","fr","hi","in","it","ja","nl","pl","ro","ru","tr","uk","zz","pt-rBR","zh-rCN","zh-rTW"
resConfigs "de"
}
buildTypes {
debug {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static android.content.Intent.EXTRA_STREAM;

public class CropAreasChooseActivity extends BaseActivity {
private static final String TAG = "ResultActivity";
private static final String TAG = "llCrop";
private static final int REQUEST_GET_PICTURE = 1;
protected static final int REQUEST_GET_PICTURE_PERMISSION = 101;

Expand All @@ -50,6 +50,7 @@ protected void onCreate(Bundle savedInstanceState) {
Uri uri = getIntent().getData();

if (uri == null) {
Log.d(TAG, "Intent.data has not initial image uri. Opening Image Picker");
// must be called with image uri
pickFromGallery();
} else {
Expand All @@ -70,8 +71,9 @@ protected void onCreate(Bundle savedInstanceState) {

uCropView.setCropRect(crop);
} catch (Exception e) {
Log.e(TAG, "setImageUri", e);
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
final String msg = "setImageUri '" + uri + "' ";
Log.e(TAG, msg, e);
Toast.makeText(this, msg + e.getMessage(), Toast.LENGTH_LONG).show();
}
}

Expand Down Expand Up @@ -159,6 +161,7 @@ private void pickFromGallery() {
getString(R.string.permission_read_storage_rationale),
REQUEST_GET_PICTURE_PERMISSION);
} else {
Log.d(TAG, "Opening Image Picker");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT)
.setType(IMAGE_JPEG_MIME)
.addCategory(Intent.CATEGORY_OPENABLE);
Expand All @@ -168,14 +171,17 @@ private void pickFromGallery() {
}
private void onGetPictureResult(int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
final Uri selectedUri = data.getData();
final Uri selectedUri = (data == null) ? null : data.getData();
if (selectedUri != null) {
Log.d(TAG, "Restarting with uri '" + selectedUri + "'");

Intent intent = new Intent(Intent.ACTION_VIEW, selectedUri, this, CropAreasChooseActivity.class);
this.startActivity(intent);
finish();
return;
}
}
Log.d(TAG, this.getString(R.string.toast_cannot_retrieve_selected_image));
Toast.makeText(this, R.string.toast_cannot_retrieve_selected_image, Toast.LENGTH_SHORT).show();
finish();
return;
Expand Down Expand Up @@ -206,20 +212,24 @@ private boolean openPublicOutputUriPicker(int folderpickerCode) {
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
;

Log.d(TAG, "openPublicOutputUriPicker '" + proposedFileName + "'");

startActivityForResult(intent, folderpickerCode);
return true;
}

private void onOpenPublicOutputUriPickerResult(int resultCode, Uri outUri) {
final Intent intent = getIntent();
final Uri inUri = ((resultCode == RESULT_OK) && (intent != null)) ? intent.getData() : null;

if (resultCode == RESULT_OK) {
final Uri inUri = getIntent().getData();
if (inUri != null) {
Rect rect = getCropRect();
InputStream inStream = null;
OutputStream outStream = null;

final String context_message = "Cropping '" + inUri + "'(" + rect + ") => '" + outUri + "'";
Log.d(TAG, context_message);
Log.i(TAG, context_message);

try {
outStream = getContentResolver().openOutputStream(outUri, "w");
Expand All @@ -233,6 +243,9 @@ private void onOpenPublicOutputUriPickerResult(int resultCode, Uri outUri) {
close(outStream, outStream);
close(inStream, inStream);
}
} else {
// uri==null or error
Log.i(TAG, "onOpenPublicOutputUriPickerResult(null): No output url, not saved.");
}
}

Expand Down Expand Up @@ -287,7 +300,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
return;
}
if (requestCode == REQUEST_SAVE_PICTURE) {
final Uri outUri = data.getData();
final Uri outUri = (data == null) ? null : data.getData();
onOpenPublicOutputUriPickerResult(resultCode, outUri);
return;
}
Expand Down

0 comments on commit e4d331e

Please sign in to comment.