From 9e4ad1495d2268fb3ef91199c205bfcbde20c933 Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Wed, 3 Jul 2019 18:57:03 +0200 Subject: [PATCH] Get external storage read permissions before selecting an image to scan Fixes #150 --- .../aegis/ui/MainActivity.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java b/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java index f01afa22..3008f8c7 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java @@ -65,6 +65,7 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene // permission request codes private static final int CODE_PERM_CAMERA = 0; + private static final int CODE_PERM_READ_STORAGE = 1; private AegisApplication _app; private DatabaseManager _db; @@ -106,12 +107,7 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene }); findViewById(R.id.fab_scan_image).setOnClickListener(view -> { _fabMenu.collapse(); - - Intent galleryIntent = new Intent(Intent.ACTION_PICK); - galleryIntent.setDataAndType(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*"); - - Intent chooserIntent = Intent.createChooser(galleryIntent, getString(R.string.select_picture)); - startActivityForResult(Intent.createChooser(chooserIntent, getString(R.string.select_picture)), CODE_SCAN_IMAGE); + startScanImageActivity(); }); findViewById(R.id.fab_scan).setOnClickListener(view -> { _fabMenu.collapse(); @@ -195,6 +191,9 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene case CODE_PERM_CAMERA: startScanActivity(); break; + case CODE_PERM_READ_STORAGE: + startScanImageActivity(); + break; } } @@ -371,6 +370,18 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene startActivityForResult(scannerActivity, CODE_SCAN); } + private void startScanImageActivity() { + if (!PermissionHelper.request(this, CODE_PERM_READ_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)) { + return; + } + + Intent galleryIntent = new Intent(Intent.ACTION_PICK); + galleryIntent.setDataAndType(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*"); + + Intent chooserIntent = Intent.createChooser(galleryIntent, getString(R.string.select_picture)); + startActivityForResult(Intent.createChooser(chooserIntent, getString(R.string.select_picture)), CODE_SCAN_IMAGE); + } + private void doShortcutActions() { Intent intent = getIntent(); String action = intent.getStringExtra("action");