Ankur Khandelwal Ankur Khandelwal - 26 days ago 7
Android Question

Preview captured Image NullpointerException

public void openDialogToAddReminder(final Context context, final DbHelper dbHelper, final int Rem_id, final int Med_id) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context);
final View mView = layoutInflaterAndroid.inflate(R.layout.add_reminders_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.myDialog));
alertDialogBuilder.setView(mView);
captureImage = (ImageButton) mView.findViewById(R.id.capture_image);
captureImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectImage(context);
}
});
alertDialogBuilder
.setCancelable(false)
.setPositiveButton(dialog_title, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
public void selectImage(final Context context) {
final CharSequence[] items = { "Take Photo", "Choose from Gallery",
"Cancel" };
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
builder.setTitle("Add Photo");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result= Utility.checkPermission(context);
if (items[item].equals("Take Photo")) {
userChoosenTask ="Take Photo";
if(result)
cameraIntent(context);
} else if (items[item].equals("Choose from Gallery")) {
userChoosenTask ="Choose from Gallery";
if(result)
galleryIntent(context);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
public void galleryIntent(Context context)
{
Log.i("Context ",context.toString());
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}
public void cameraIntent(Context context)
{
Intent takingPictureCameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takingPictureCameraintent.resolveActivity(context.getPackageManager())!=null)
startActivityForResult(takingPictureCameraintent, REQUEST_CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
public void onCaptureImageResult(Intent data)
{
try{
Bundle extras=data.getExtras();
Bitmap thumbnail = (Bitmap) extras.get("data");
Log.i("Image Camera Bitmap ",thumbnail.toString());
ByteArrayOutputStream bytes=new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90,bytes);
captureImage.setImageBitmap(thumbnail);
saveToGallery(thumbnail);
}
catch (Exception e){e.printStackTrace();}
}


add_reminders_dialog.xml



<?xml version = "1.0" encoding="utf-8"?>
<RelativeLayout android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:layout_marginTop="@dimen/activity_horizontal_margin"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/r1"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
android:id="@+id/cardview_medicine_image"
android:layout_marginTop="20dp"
app:contentPadding="@dimen/activity_horizontal_margin"
android:layout_below="@+id/cardview_medicine_time"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageButton
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="#424242"
android:padding="10dp"
android:clickable="true"
android:src="@drawable/icon_camera"
android:scaleType="fitCenter"
android:id="@+id/capture_image"/>
</android.support.v7.widget.CardView>
</RelativeLayout>


Getting NullPointerException here:

captureImage.setImageBitmap(thumbnail);


I don't know why I am getting captureImage null. As I have define it globally. Why it is null as I have defined it inside the dialog box.

Answer

Well your captureImage belongs to the view of your first alert dialog (the one created in openDialogToAddReminder method.

I suspect that upon clicking on the captureImage to select the image, this dialog gets dismissed so it destroys the captureImage reference, hence when the onActivityResult is called there is no more captureImage object.

You can check the above if you set a dismiss listener on your AlertDialog.Builder e.g.

 alertDialogBuilder.setOnDismissListener(new OnDismissListener() {

    @Override
    public void onDismiss(DialogInterface dialog) {
        Log.d("onDismiss");
    }
});

place this code right above:

AlertDialog alertDialog = alertDialogBuilder.create();

EDIT:
If you receive on your logcat: "onDismiss" this means that my assumption is correct. Try to resolve it by:

alertDialog.setCanceledOnTouchOutside(false);