0
我已經從我的活動啓動相機應用程序下面的代碼:相機照片拍攝失敗
private Uri imageUri;
private void takeCameraPhoto() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
}
當用戶拍攝照片並選擇它,我,我有我的onActivtyResult方法得到的回調於Android這樣寫:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
tempView.setVisibility(View.INVISIBLE);
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
try {
imageBMP = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
String newFilePath = selectedImage.toString().replace("file:///mnt/", "/");
imageBMP = ShrinkBitmap(newFilePath, width, height);
imageView.setImageBitmap(imageBMP);
Matrix m1= imageView.getImageMatrix();
RectF drawableRect1 = new RectF(0, 0, imageBMP.getWidth(), imageBMP.getHeight());
RectF viewRect1 = new RectF(0, 0, width - 50, height - 100);
m1.setRectToRect(drawableRect1, viewRect1, Matrix.ScaleToFit.CENTER);
matrix.set(m1);
imageBMP = null;
System.gc();
/*Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();*/
}else{
if (!isFromDialog) {
PhotoDecorationActivity.this.finish();
}
}
break;
搭載Android 4.4版本,我得到的NullPointerException這imageBMP爲null,這是因爲空newFilePath變量的可能。什麼是正確的代碼捕捉從相機的圖像和接收它在我的activity.Can人幫助我請。
是什麼Add_customer.RESULT_OK – Sandeep 2014-09-10 10:49:06
對不起它的Activity.RESULT_OK ....我現在編輯我的答案...您也可以使用您的活動名稱 – danidee 2014-09-10 10:58:23