0

Let's say I'm trying to save a bitmap image to a png

public void save() {
  String filename = "file.png";
        File sd = Environment.getExternalStorageDirectory();
        File dest = new File(sd, filename);

        try {

            FileOutputStream out = new FileOutputStream(dest);
            fBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
}

If I use Android 6.0 and above, I need to ask for runtime permissions

void validatePermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

            } else {

                ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);

            }

        }
  }

I have a few questions:

The above code successfully asks for permissions, however I have to re-start the app, How do I porperly halt the code until permissions are either granted or not granted?

Below Android 6.0, permissions are granted on install by the manifest file. How does android 5 or lower handle runtime permissions code?

Thanks

tosi
  • 611
  • 9
  • 24

3 Answers3

0

You shouldn't restart the app. You should change a logic of your app: wait when user grants the permission and try to run an operation a second time. And, yes, you can use third-party libraries for this purpose. For example, my personal choice: Permission dispatcher.

Alex Shevelev
  • 681
  • 7
  • 14
0

The above code successfully asks for permissions, however I have to re-start the app, How do I porperly halt the code until permissions are either granted or not granted?

You can use this void

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.          
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
            }
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

from this answer

Below Android 6.0, permissions are granted on install by the manifest file. How does

android 5 or lower handle runtime permissions code?

The code will be skipped

Community
  • 1
  • 1
5ec20ab0
  • 742
  • 6
  • 15
0

You can use Dexter library for better performance. I am sharing here both Dexter code also

Using Dexter, you need to add Dexterdependency in your app.build gradle file.

implementation 'com.karumi:dexter:6.2.2'

Single permission/////
Dexter.withContext(activity)
            .withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            .withListener(object : PermissionListener {
                override fun onPermissionGranted(p0: PermissionGrantedResponse?) {
                    downloadImage(url)
                }

                override fun onPermissionDenied(p0: PermissionDeniedResponse?) {
                    if (p0!!.isPermanentlyDenied) {
                        showSettingsDialog()
                    }
                }

                override fun onPermissionRationaleShouldBeShown(
                        p0: PermissionRequest?, p1: PermissionToken?
                ) {
                    p1!!.continuePermissionRequest()
                }

            })
            .onSameThread()
            .check()


Multiple Permission////

Dexter.withContext(requireContext())
            .withPermissions(Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE)
            .withListener(object : MultiplePermissionsListener {
                override fun onPermissionsChecked(p0: MultiplePermissionsReport?) {
                    if (p0!!.areAllPermissionsGranted())
                    {
                        
                        
                    }else if (p0.isAnyPermissionPermanentlyDenied)
                    {
                        openSettings()
                    }
                }

                override fun onPermissionRationaleShouldBeShown(p0: MutableList<PermissionRequest>?, permissionToken: PermissionToken?) {
                    permissionToken?.continuePermissionRequest()
                }
            }).check()
David Buck
  • 3,752
  • 35
  • 31
  • 35
Siru malayil
  • 197
  • 2
  • 11