从 android 中的意图选择器选择选项(相机或画廊)后请求权限
Request Permission after selected option(either camera or gallery) from intent chooser in android
我创建了一个包含图库、照片和相机应用程序的意图选择器。现在,对于 运行ning Android 6.0 或更高版本的设备,我想询问 运行 应用程序从选择器 selected 之后的时间权限,例如用户 select s 图库选项我将仅询问存储权限,如果用户 select 相机选项,我将询问相机和存储权限(如果之前未提供)。
有人可以帮我这样做吗?
这是我的代码
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);
}
随时使用此代码检查权限。
数组需要什么权限。
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;
}
}
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);
}
}
请自行扩展,在需要一次性请求多个权限的地方使用Manifest.permission_group代替Manifest.permission
https://developer.android.com/training/permissions/requesting.html
感谢大家的支持。
我自己解决了。
我在上面的方法中附加了下面的代码
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),用于通知选择了哪个 Intent 选择器选项
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));
}
}
}
不要忘记在清单中输入您的广播接收器。
我创建了一个包含图库、照片和相机应用程序的意图选择器。现在,对于 运行ning Android 6.0 或更高版本的设备,我想询问 运行 应用程序从选择器 selected 之后的时间权限,例如用户 select s 图库选项我将仅询问存储权限,如果用户 select 相机选项,我将询问相机和存储权限(如果之前未提供)。
有人可以帮我这样做吗?
这是我的代码
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);
}
随时使用此代码检查权限。
数组需要什么权限。
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;
}
}
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);
}
}
请自行扩展,在需要一次性请求多个权限的地方使用Manifest.permission_group代替Manifest.permission
https://developer.android.com/training/permissions/requesting.html
感谢大家的支持。
我自己解决了。
我在上面的方法中附加了下面的代码
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),用于通知选择了哪个 Intent 选择器选项
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));
}
}
}
不要忘记在清单中输入您的广播接收器。