0

I have made an app for Android (target SDK 27) that uses 2 modules, one made by me and another one has been imported. This last module requires the use of CAMERA and EXTERNAL STORAGE (read/write), therefore the manifest file includes the necessary lines.

Also, I check and ask during runtime for these permissions and user is able to grant or not access. When I go to "App settings", both CAMERA and STORAGE are turned on. Everything seems fine.

However, the app crashes after taking a picture with the camera or selecting one from the gallery.

On the other side, If I grant these permissions manually on the App settings, everything works fine!

Any idea?

Manifest permissions

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

TEST Thread continuously checking for permissions before Activity execution

    private void checkPermissions(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(!hasStorageAccess()){
                    requestStoragePermission();
                }
                while (!hasCameraAccess()) {
                    requestCameraPermission();
                }
                init();
            }
        }).start();
    }

Runtime permissions (Work perfectly on other apps made by me)

    private boolean hasCameraAccess() {
        return ContextCompat.checkSelfPermission(ScanActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
    }

    private void requestCameraPermission() {
        ActivityCompat.requestPermissions(ScanActivity.this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_USE_CAMERA);
    }

    private boolean hasStorageAccess() {
        return ContextCompat.checkSelfPermission(ScanActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    }

    private void requestStoragePermission() {
        ActivityCompat.requestPermissions(ScanActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_USE_STORAGE);
    }
eyllanesc
  • 190,383
  • 15
  • 87
  • 142
  • Why are you using that `Thread` construct for your permissions check and request? Those request calls are not blocking, so those loops are going to needlessly spin repeatedly. I'd imagine that might be causing problems with the system components that actually handle the permissions. – Mike M. Aug 19 '19 at 10:01
  • It's just an example for the permissions request, I just use it for testing. I think it has nothing to do with my problem. I also tried it without threads or loops without success... – John Walkers Aug 19 '19 at 10:05
  • OK. Well, anyway, if it's crashing _after_ the using the camera, then you're getting the `CAMERA` permission just fine. What's in your [stack trace](https://stackoverflow.com/a/23353174)? – Mike M. Aug 19 '19 at 10:08
  • Oh, wait, I think I see it. You're only requesting `READ_EXTERNAL_STORAGE`. You need to explicitly request `WRITE_EXTERNAL_STORAGE`. Since Oreo, you don't get the whole permission group when the user grants only one of them: https://stackoverflow.com/a/47788508. However, when you enable it manually, it does grant the whole group. – Mike M. Aug 19 '19 at 10:12
  • 1
    WOW! I thought I tried it before... That's the answer: requesting the WRITE_EXTERNAL_STORAGE explicitly too... Thank you very much! – John Walkers Aug 19 '19 at 10:17
  • you can just put asking permission at on create activity by below lines if(storagePermission!=PackageManager.PERMISSION_GRANTED) { listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } – yuvrajsinh Aug 21 '19 at 11:58

0 Answers0