diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f7b0168..a5ac7ef 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,9 @@ + + + = Build.VERSION_CODES.JELLY_BEAN + && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE, + getString(R.string.permission_read_storage_rationale), + REQUEST_GET_PICTURE_PERMISSION); + } else { + Intent intent = new Intent(Intent.ACTION_GET_CONTENT) + .setType("image/jpeg") + .addCategory(Intent.CATEGORY_OPENABLE); + + startActivityForResult(Intent.createChooser(intent, getString(R.string.label_select_picture)), REQUEST_GET_PICTURE); + } + } + private void onGetPictureResult(int resultCode, Intent data) { + if (resultCode == RESULT_OK) { + final Uri selectedUri = data.getData(); + if (selectedUri != null) { + Intent intent = new Intent(Intent.ACTION_VIEW, selectedUri, this, MainActivity.class); + this.startActivity(intent); + finish(); + return; + } + } + Toast.makeText(this, R.string.toast_cannot_retrieve_selected_image, Toast.LENGTH_SHORT).show(); + finish(); + return; + } + + /** + * Callback received when a permissions request has been completed. + */ + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + switch (requestCode) { + case REQUEST_GET_PICTURE_PERMISSION: + if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + pickFromGallery(); + } + break; + default: + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + } + } + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_GET_PICTURE) { + onGetPictureResult(resultCode, data); + return; + } + super.onActivityResult(requestCode, resultCode, data); + } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d5b881a..d053f0f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -8,15 +8,6 @@ tools:context=".MainActivity" > - - Loss-Less-Crop + + Select Picture + + Permission needed + Storage read permission is needed to pick files. + Storage write permission is needed to save the image. + + Cannot retrieve selected image + Cannot retrieve cropped image + Unexpected error + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 5885930..f927816 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,7 +1,7 @@ -