activesince93 activesince93 - 21 days ago 8
Android Question

Application crashes when pressing back in camera

I am having problem in my application.
Its crashing when I press back button in camera without capturing image.
I can not understand what is the problem.
Any one of you please give me solution for this.

This is code when button is pressed:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("From where to select Image?")
.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GlobalVariables.window="end";
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
try {
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);

} catch (ActivityNotFoundException ane) {
ane.printStackTrace();
}

}
})
.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

GlobalVariables.window="end";
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(RespAdap.path.toString()+"/Attachment.png")));
startActivityForResult(intent, 1337);

}
});

AlertDialog dialog = builder.create();
dialog.show();


This is code of onActivityResult:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == SELECT_PICTURE) {
if(resultCode != 0)
{
if (bitmap != null) {
bitmap.recycle(); // If bitmap is not null then here it will be recycled and set to null
bitmap = null;

}
try{
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
File fileObject = new File(selectedImagePath);
bitmap = decodeFile(fileObject);
img_selected.setImageBitmap(bitmap);
String val = getBase64Value(bitmap);
myBitmapImage=bitmap;
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
fileExt = filePath.substring(filePath.length()-3);
}
cursor.close();
img_selected.setVisibility(View.VISIBLE);
}
catch (Exception e) {
// TODO: handle exception
}
//new uploadImage().execute(val);
}
}
else if (requestCode == 1337) {
if (data == null&&resultCode==-1) {
try{File fileObject = new File(RespAdap.path.toString()+"/Attachment.png");
bitmap = decodeFile(fileObject);
//img_view.setImageBitmap(bitmap);
String val = getBase64Value(bitmap);
Matrix matrix = new Matrix();
matrix.postRotate(getCameraPhotoOrientation(getActivity(),Uri.fromFile(new File(RespAdap.path.toString()+"/Attachment.png")), RespAdap.path.toString()+"/Attachment.png"));
bitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(),
matrix, true);
myBitmapImage=bitmap;
img_selected.setVisibility(View.VISIBLE);
img_selected.setImageBitmap(bitmap);
}
catch (Exception e) {
// TODO: handle exception
}

} else {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
int imageHeight = thumbnail.getHeight();
int imageWidth = thumbnail.getWidth();
Log.v("Log_tag", "Final image Height" + imageHeight);
Log.v("Log_tag", "Final image Width" + imageWidth);
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
fileExt = filePath.substring(filePath.length()-3);
}
cursor.close();
String selectedImagePath = getPath(selectedImage);
File fileObject = new File(selectedImagePath);
bitmap = decodeFile(fileObject);
//img_view.setImageBitmap(bitmap);
String val = getBase64Value(bitmap);
Matrix matrix = new Matrix();
matrix.postRotate(getCameraPhotoOrientation(getActivity(), selectedImage, selectedImagePath));
bitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(),
matrix, true);
myBitmapImage=bitmap;
img_selected.setVisibility(View.VISIBLE);
img_selected.setImageBitmap(bitmap);
}
}
super.onActivityResult(requestCode, resultCode, data);
}


I am testing on Galaxy Note 10.1. This is the logcat for that.

09-11 20:48:24.896: E/AndroidRuntime(30747): FATAL EXCEPTION: main
09-11 20:48:24.896: E/AndroidRuntime(30747): Process: com.first.ogoing, PID: 30747
09-11 20:48:24.896: E/AndroidRuntime(30747): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=329017, result=0, data=null} to activity {com.first.ogoing/com.first.try_flymenu.ReponsiveList}: java.lang.NullPointerException
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread.access$1400(ActivityThread.java:169)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.os.Handler.dispatchMessage(Handler.java:102)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.os.Looper.loop(Looper.java:136)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread.main(ActivityThread.java:5476)
09-11 20:48:24.896: E/AndroidRuntime(30747): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 20:48:24.896: E/AndroidRuntime(30747): at java.lang.reflect.Method.invoke(Method.java:515)
09-11 20:48:24.896: E/AndroidRuntime(30747): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
09-11 20:48:24.896: E/AndroidRuntime(30747): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
09-11 20:48:24.896: E/AndroidRuntime(30747): at dalvik.system.NativeStart.main(Native Method)
09-11 20:48:24.896: E/AndroidRuntime(30747): Caused by: java.lang.NullPointerException
09-11 20:48:24.896: E/AndroidRuntime(30747): at com.first.try_flymenu.adapter.RespChangImage.onActivityResult(RespChangImage.java:258)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:161)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.Activity.dispatchActivityResult(Activity.java:5665)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
09-11 20:48:24.896: E/AndroidRuntime(30747): ... 11 more

Answer

You have a nullpointerexception in your receiving activity:

at

com.first.try_flymenu.adapter.RespChangImage.onActivityResult(RespChangImage.java:258)

At line 258, it seems as you expect data to be returned that is not there.

You need to check in your onActivityResult() that if(resultcode == RESULT_OK) before handling the data.

Comments