Request Permission after selected option(either camera or gallery) from intent chooser in android

余生颓废 提交于 2019-12-03 23:12:29

Thanks to all for your support.

I solved it myself.

I appended below code in above method

Intent receiver = new Intent(MainActivity.this, IntentOptionReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
        //Create the Chooser
        final Intent chooserIntent;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            chooserIntent = Intent.createChooser(galleryIntent, "Select Source", pendingIntent.getIntentSender());
        } else {
            chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
        }

Here is my broadcast receiver(IntentOptionReceiver) to notify which intent chooser option selected

public class IntentOptionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        for (String key : intent.getExtras().keySet()) {
            Log.e("intentOptionReceiver", "Intent option clicked info" + intent.getExtras().get(key));
        }
    }
}

Do not forget to enter your broadcast receiver inside manifest.

check permission using this code when ever.

what permission need give in to the array.

if ((ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);

    }

add this method also

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSIONS_CODE:
                if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                break;
        }
    }

This method returns true if the app has Read External Storage Permission

 private boolean hasReadExternalStoragePermission() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                int permissionCheck = checkSelfPermission(getActivity(),
                        Manifest.permission.READ_EXTERNAL_STORAGE);
                if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE);
                    return false;
                }
                return true;
            }
            return true;
        }

Wrap the actions that require permission with hasReadExternalStoragePermission method.

 public void openGallery() {

        if (hasReadExternalStoragePermission()) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, REQUEST_IMAGE_FROM_GALLERY);
        }
    }

Please expand more on your own, use Manifest.permission_group instead of Manifest.permission, In places where you need to request multiple permission at once.

https://developer.android.com/training/permissions/requesting.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!