从Android中的意图选择器中选择选项(相机或图库)后请求权限
我创建了一个包含图库,照片和相机应用程序的意向选择器。现在对于在Android 6.0或更高版本上运行的设备,我希望在从选择器中选择应用后询问运行时权限,就像用户选择图库选项一样,我只会询问存储权限,如果用户选择相机选项,我会询问相机和存储如果之前没有给出这两个权限。从Android中的意图选择器中选择选项(相机或图库)后请求权限
有人可以帮我这么做吗?
这里是我的代码使用此代码当过
public void openImageIntent() {
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fname = "ABCD_" + timeStamp;
final File sdImageMainDirectory = new File(storageDir, fname);
fileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
cameraIntents.add(intent);
}
//Gallery.
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//Create the Chooser
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, 65530);
}
感谢大家的支持。
我自己解决了。
我追加下面的代码在上述方法中
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");
}
这里是我的广播接收器(IntentOptionReceiver),通知其意图选择期权选择
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));
}
}
}
不要忘了进入里面体现你的广播接收器。
检查权限。
什么权限需要放入数组中。
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);
}
添加这种方法也
@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;
}
}
感谢您的答案,但我怎么会知道从意向选择器(相机或画廊)选择哪个应用程序。我无法从意向选择器中获取选定的意图。 –
@SapnaSharma你必须要求两个权限,因为你不能假设哪一个用户会选择。 –
你是对的@Nitin,但改变个人资料图片时,whatsapp处理它。我不知道怎么。 –
如果应用程序具有读取外部存储许可
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;
}
裹需要许可与操作此方法返回true hasReadExternalStoragePermission方法。
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);
}
}
请扩展更多的你自己,用Manifest.permission_group代替Manifest.permission,在地方,你需要一次申请多个权限。
https://developer.android.com/training/permissions/requesting.html
这个问题不仅是关于询问运行时权限。可能是我没有解释得很好。我想在用户从意向选择器选择应用程序之后立即询问存储或摄像头权限,具体取决于用户选择的应用程序。 –
仅当您有相机选择器意图无法打开的存储权限时打电话才能打开相机。如果您没有存储权限 –
[在运行时询问权限,Android M +]可能的重复(https://stackoverflow.com/questions/42608731/asking-for-permissions-at-run-time-android-m) –
尝试和检查这一个:https://theartofdev.com/2015/02/15/android-cropping-image-from-camera-or-gallery/ –
谢谢@Nitin。此链接https://stackoverflow.com/questions/32203230/how-to-tell-which-app-was-selected-by-intent-createchooser/32203659#32203659由你分享帮助我。 –