从Android中的意图选择器中选择选项(相机或图库)后请求权限 [英] Request Permission after selected option(either camera or gallery) from intent chooser in android

查看:238
本文介绍了从Android中的意图选择器中选择选项(相机或图库)后请求权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含图库,照片和相机应用程序的意图选择器.现在,对于在Android 6.0或更高版本上运行的设备,我想从选择器中选择应用后询问运行时权限,例如用户是否选择图库选项,我将仅询问存储权限,如果用户选择相机选项,则询问相机和存储空间这两个权限(如果之前未提供).

I have created an intent chooser containing Gallery, Photos and Camera apps. Now for the devices running on Android 6.0 or greater I want to ask the run time permissions after app is selected from chooser like if user selects gallery option I will ask storage permission only and in case if user select camera option I will ask camera and storage both permissions if not given before.

有人可以帮我吗?

这是我的代码

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);
}

推荐答案

感谢大家的支持.

我自己解决了.

我在上述方法中附加了以下代码

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");
        }

这是我的广播接收器(IntentOptionReceiver),用于通知选择了哪个意图选择器选项

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.

这篇关于从Android中的意图选择器中选择选项(相机或图库)后请求权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆