Reputation:
The camera function in my app really driving me in nuts !!!
I have built an camera function in my app, but it crashed when I try to select the image from gallery.
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();
}
}
}
If I remove the activeTakePhoto()
and all the line after case REQUEST_IMAGE_CAPTURE:
, the selected image can be displayed on imageView
.
If I remove activeGallery()
, the captured image can be display on imageView
.
But, if I use the snippet code I posted here (both function), it crashed when I select an image from gallery with the logCat
error
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)
and the line is Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
. However, when I select capture image, the captured image still can display on imageView
.
Upvotes: 0
Views: 3437
Reputation: 161
you are missing break; between switch cases block.
Once break is put, you will not get exception as fall through will not happen to case 'REQUEST_IMAGE_CAPTURE'
Upvotes: 1