我从Galery和相机应用中挑选图片时遇到问题。我关注this tutorial。问题是Camera chooserIntent没有在Android M上启动(其他版本是启动),但Galery意图是Launch。那怎么解决呢?
这是chooserIntent:
public static Intent getPickImageIntent(Context context) {
Intent chooserIntent = null;
List<Intent> intentList = new ArrayList<>();
Intent pickIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotoIntent.putExtra("return-data", true);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
intentList = addIntentsToList(context, intentList, pickIntent);
intentList = addIntentsToList(context, intentList, takePhotoIntent);
if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
context.getString(R.string.pick_image_intent_text));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
}
return chooserIntent;
}
并实现代码:
public class SimpleActivity {
private static final int PICK_IMAGE_ID = 234; // the number doesn't matter
public void onPickImage(View view) {
Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case PICK_IMAGE_ID:
Bitmap bitmap = ImagePicker.getImageFromResult(this, resultCode, data);
// TODO use bitmap
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
}
答案 0 :(得分:0)
对于打开相机意图,您可以使用它。
{{1}}
答案 1 :(得分:0)
编辑:运行时权限只是一个问题。我想知道为什么当你没有要求获得CAMERA许可时你的应用程序没有崩溃,我想通了。这是因为你从不打开相机:
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
context.getString(R.string.pick_image_intent_text));
您删除了intentList
中的最后一个意图,即takePhotoIntent
。因此intentList
仅包含pickIntent
。这就是原因。
Android M附带Runtime Permission。在应用程序可以使用它们之前,用户需要授予某些权限。在这种情况下,他们会CAMERA
和WRITE_EXTERNAL_STORAGE
。所以你必须要求权限:
public void onPickImage(View view) {
askForCameraPermission();
}
private void askForCameraPermission() {
// Neither CAMERA or WRITE_EXTERNAL_STORAGE is granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)
|| ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Permissions needed");
builder.setMessage("Some explanations");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// User understands the situation, now ask for permissions
ActivityCompat.requestPermissions(SimpleActivity.this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_CAMERA);
}
});
builder.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_CAMERA);
}
} else {
// Permissions are already granted before
Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
// Blame user
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Error");
builder.setMessage("Y u do dis?!");
builder.setPositiveButton("Nah", null);
builder.show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
答案 2 :(得分:0)
使用Android M,运行时权限会发生重大变化。要使用相机,您必须在运行时授予权限。看一下这个链接,它解释了如何处理权限请求流程: