我正在尝试从7.0设备上的相机裁剪图像。我已经使用android Intents编写了crop功能,并通过文件提供程序公开了我的文件以避免FileUriExposedException。
问题是在打开相机后,裁剪工具没有显示,在我的imageview中没有显示任何内容。
这是我的裁剪功能代码:
private void cropImage() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(uri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("outputX", 200);
cropIntent.putExtra("outputY", 200);
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CROP_CODE);
} catch (ActivityNotFoundException aex) {
aex.printStackTrace();
Toast.makeText(this, "Current Device Has No Crop Support", Toast.LENGTH_LONG).show();
}
}
和我的相机意图代码在这里:
private void photoDialog() {
final CharSequence[] imgOptions = {"Take Photo", "Select from Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Image Options");
builder.setItems(imgOptions, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (imgOptions[i].equals("Take Photo")) {
try {
Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(), "tmp_" + String.valueOf(System.currentTimeMillis()));
Log.d("MainActivity", "File Saved in:\t" + file.getAbsolutePath().toString());
if (Build.VERSION.SDK_INT > 23) {
uri = FileProvider.getUriForFile(MainActivity.this, getString(R.string.file_prov_authority), file);
} else {
uri = Uri.fromFile(file);
}
camIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
camIntent.putExtra("return-data", "true");
startActivityForResult(camIntent, CAM_CODE);
} catch (ActivityNotFoundException ex) {
ex.printStackTrace();
}
} else if (imgOptions[i].equals("Select from Gallery")) {
Intent galIntent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(galIntent, "Open With"), GAL_CODE);
}
}
});
当我尝试直接从相机位图设置imageview时,我也得到了这个例外:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
我的活动结果代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAM_CODE && resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
img_camera.setImageBitmap(bitmap);
cropImage();
}
else if (requestCode == GAL_CODE && resultCode == RESULT_OK) {
if (data != null) {
uri = data.getData();
img_camera.setImageURI(uri);
cropImage();
}
}
else if (requestCode == CROP_CODE) {
Bundle photo_extras = data.getExtras();
Bitmap bitmap = photo_extras.getParcelable("data");
img_camera.setImageBitmap(bitmap);
}
}
来自图库代码的我的作品虽然功能相同但效果很好。
任何人都可以帮助我在Nougat设备上裁剪相机图像,因为我已经抓取了每个博客和YT视频和ntn到目前为止工作。感谢。
答案 0 :(得分:0)
我认为你的问题在于你的形象名称。尝试使用.jpg命名为:
file = new File(Environment.getExternalStorageDirectory(), "tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
你的错误异常来自这一行:
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
将其更改为:
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
如果它不起作用,请尝试我的代码(这对我有用。我希望它对你也有用):
//Define constants:
private static final int PERMISSION_CAMERA_CODE = 1;
private static final int PERMISSION_STORE_CODE = 2;
private static final int REQUEST_CODE_TAKE_IMAGE = 3;
private static final int REQUEST_CODE_CHOOSE_IMAGE = 4;
private static final int REQUEST_CODE_CROP_AVATAR = 5;
private static final int REQUEST_CODE_CROP_COVER = 6;
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
file = new File(Environment.getExternalStorageDirectory(), "file" + String.valueOf(System.currentTimeMillis()) + ".jpg");
uri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CODE_TAKE_IMAGE);
}
private void openGallery() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select image from your gallery"), REQUEST_CODE_CHOOSE_IMAGE);
}
然后:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_TAKE_IMAGE && resultCode == Activity.RESULT_OK) {
cropImage();
} else if (requestCode == REQUEST_CODE_CHOOSE_IMAGE && resultCode == Activity.RESULT_OK) {
if (data != null) {
uri = data.getData();
cropImage();
}
} else if (requestCode == REQUEST_CODE_CROP_AVATAR && resultCode == Activity.RESULT_OK) {
if (data != null) {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
ivAvatar.setImageBitmap(bitmap);
}
}
}
private void cropImage() {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(uri, "image/*");
cropIntent.putExtra("crop", true);
cropIntent.putExtra("outputX", 180);
cropIntent.putExtra("outputY", 180);
cropIntent.putExtra("aspectX", 3);
cropIntent.putExtra("aspectY", 3);
cropIntent.putExtra("scaleUpIfNeeded", true);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, REQUEST_CODE_CROP_AVATAR);
}