我正在尝试在拍摄后裁剪图像,我的代码如下:
private void doTakePhotoAction() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
//Do nothing for now
}
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我可以进入裁剪模式,裁剪图片.但是,不强制执行1:1宽高比,并且outputX和outputY都不是.我相信这是因为意图是拍照,而不是剪裁.我还从Intent中编写了另一个getData()方法,然后使用以下方法:
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我得到以下运行时错误:
E/AndroidRuntime(14648): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.camera/com.android.camera.CropImage}: java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!:)
Wys*_*sie 22
在做了一些阅读之后,我意识到它不能这么简单地完成.我的modded联系人来源是http://github.com/Wysie,你可以看一下你是否感兴趣.此外,这是我做的工作:
private void doTakePhotoAction() {
// http://2009.hfoss.org/Tutorial:Camera_and_Gallery_Demo
// http://stackoverflow.com/questions/1050297/how-to-get-the-url-of-the-captured-image
// http://www.damonkohler.com/2009/02/android-recipes.html
// http://www.firstclown.us/tag/android/
// The one I used to get everything working: http://groups.google.com/group/android-developers/msg/2ab62c12ee99ba30
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Wysie_Soh: Create path for temp file
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
//Do nothing for now
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case CROP_FROM_CAMERA: {
//Wysie_Soh: After a picture is taken, it will go to PICK_FROM_CAMERA, which will then come here
//after the image is cropped.
final Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
mPhoto = photo;
mPhotoChanged = true;
mPhotoImageView.setImageBitmap(photo);
setPhotoPresent(true);
}
//Wysie_Soh: Delete the temporary file
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) {
f.delete();
}
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mPhotoImageView, InputMethodManager.SHOW_IMPLICIT);
break;
}
case PICK_FROM_CAMERA: {
//Wysie_Soh: After an image is taken and saved to the location of mImageCaptureUri, come here
//and load the crop editor, with the necessary parameters (96x96, 1:1 ratio)
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_FROM_CAMERA);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你 :)
您是否尝试过此操作Intent(但保留已有的作物/方面/输出/返回数据额外内容)?
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Run Code Online (Sandbox Code Playgroud)
这基本上就是Android 联系人应用程序的功能,因此它可能不太适合您的用例(即立即拍照,而不是从图库中选择一张照片或拍摄一张新照片)。
无论如何值得一试!:)
| 归档时间: |
|
| 查看次数: |
59444 次 |
| 最近记录: |