How to Ask Permission to Access Gallery on Android M.

How to set permission to device gallery?

System permissions are divided into two categories, normal and dangerous:

1-Normal permissions do not directly risk the user's privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.

2-Dangerous permissions can give the app access to the user's confidential data. If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, the user has to explicitly give approval to your app.

So in your manifst you are asking for dangerous permissions, it's normal the device ask if you want to give that permission

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

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.

is permission needed when choosing image from gallery on android?

No you do not need any permission using ACTION_PICK, ACTION_GET_CONTENT or ACTION_OPEN_DOCUMENT.

Permission for an image from Gallery is lost after re-launch

So, with significant help of @greenapps and after the testing on the emulator and real devices, the solution is this:

  • use another intent:

    Intent photoPickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

  • in onActivityResult() secure the persistable permission for Uri which will be intact after the app's re-launch:

    Uri selectedImage = data.getData();

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
    getContentResolver().takePersistableUriPermission (selectedImage, Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    }



Related Topics



Leave a reply



Submit