从相机裁剪图像(android)
Posted
技术标签:
【中文标题】从相机裁剪图像(android)【英文标题】:Crop image from camera(android) 【发布时间】:2015-09-09 09:55:16 【问题描述】:对不起我的英语。我想使用相机和裁剪图像。但是有错误:
Caused by: java.lang.NullPointerException
at com.bottom.smart.cropimage.MainActivity.onActivityResult(MainActivity.java:87)
at android.app.Activity.dispatchActivityResult(Activity.java:4663)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3131)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3178)
at android.app.ActivityThread.access$1100(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4624)
at java.lang.reflect.Method.invokeNative(Native Method)
第 87 行:Bitmap photo = extras.getParcelable("data");
我想这样做:单击按钮,打开相机,拍照,裁剪此图像并在 mainActivity 中输出裁剪图像。吼吼我的代码
我的代码:
private static final int PICK_FROM_CAMERA = 1;
ImageView imgview;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgview = (ImageView) findViewById(R.id.imageView1);
Button buttonCamera = (Button) findViewById(R.id.btn_take_camera);
buttonCamera.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
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", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
try
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
catch (Exception e)
Log.e("error", e.toString());
);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == PICK_FROM_CAMERA)
Bundle extras = data.getExtras();
if (data != null && data.getExtras() !=null)
Bitmap photo = extras.getParcelable("data");
imgview.setImageBitmap(photo);
【问题讨论】:
使用这个Bitmap yourImage = (Bitmap) data.getExtras().get("data");
@kishorejethava 感谢您的回答,现在我在Bundle extras = data.getExtras();
中有Null pointer exception
code.tutsplus.com/tutorials/…
您应该将 `Bundle extras = data.getExtras();` 移动到 if 块中,在该块中检查数据是否为空。这可能无法解决您的问题,但您可以摆脱异常。
您确定"data"
确实是正确的密钥吗?
【参考方案1】:
我做过类似的事情。
Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
SaveImageTask.SaveImageListener saveCameraImageListener = new SaveImageTask
.SaveImageListener()
@Override
public void onImageSaved(String filePath)
Uri uri = Uri.fromFile(new File(filePath));
beginCrop(uri);
;
SaveImageTask saveTask = new SaveImageTask(saveCameraImageListener);
saveTask.execute(cameraBitmap);
private void beginCrop(Uri source)
Uri outputUri = Uri.fromFile(new File(getCacheDir(), "cropped" + System.currentTimeMillis
()));
new Crop(source).output(outputUri).asSquare().start(this);
【讨论】:
以上是关于从相机裁剪图像(android)的主要内容,如果未能解决你的问题,请参考以下文章