我的应用程序中的相机功能确实让我疯狂!
我在我的应用程序中构建了一个摄像头功能,但是当我尝试从图库中选择图像时它崩溃了。
Bitmap photo;
private static final int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
ImageView imageView;
private void activeTakePhoto() { // if select open camera
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
private void activeGallery() { // if select choose from gallery
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap a = (BitmapFactory.decodeFile(picturePath));
photo = scaleBitmap(a, 200, 200);
imageView.setImageBitmap(photo);
//photo = decodeSampledBitmapFromUri(picturePath, 100, 20);
imageView.setImageBitmap(photo);
}
case REQUEST_IMAGE_CAPTURE:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
//to generate random file name
String fileName = "tempimg.jpg";
try {
photo = (Bitmap) data.getExtras().get("data");
//captured image set in imageview
imageView.setImageBitmap(photo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果我删除了activeTakePhoto()
以及case REQUEST_IMAGE_CAPTURE:
之后的所有行,则可以在imageView
上显示所选图像。
如果我删除activeGallery()
,则可以在imageView
上显示捕获的图像。
但是,如果我使用我在此处发布的代码段(两个功能),当我从库中选择logCat
错误
12-10 14:49:03.507 927-927/? E/HwSystemManager﹕ :ACTION_BATTERY_CHANGED pluged =2
12-10 14:49:07.020 19428-19428/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/832821 flg=0x1 }} to activity {com.example.project.myapplication/com.example.project.myapplication.GUI.AddMoreClaims}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3579)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3622)
at android.app.ActivityThread.access$1100(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1436)
,该行为Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
。但是,当我选择捕获图像时,捕获的图像仍然可以显示在imageView
。
答案 0 :(得分:1)