Ankur Khandelwal Ankur Khandelwal - 28 days ago 8
Android Question

Camera not working

I have MainActivity in which I have all the function related to camera.
When I am calling the camera open function from mainactivity is working fine. But when I am calling the main activity camera function through adaptor it is giving below error:-


1-10 15:53:48.494 5741-5741/user.com.test2 E/InputEventReceiver:
Exception dispatching input event. 11-10 15:53:48.494
5741-5741/user.com.test2 E/MessageQueue-JNI: Exception in MessageQueue
callback: handleReceiveCallback 11-10 15:53:48.494
5741-5741/user.com.test2 E/MessageQueue-JNI:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
at android.app.Activity.startActivityForResult(Activity.java:3794)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:3755)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
at user.com.hlthee.Reminders.galleryIntent(Reminders.java:1049)
at user.com.hlthee.Reminders$24.onClick(Reminders.java:1033)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1082)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3072)
at android.widget.AbsListView.onTouchUp(AbsListView.java:3920)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3657)
at android.view.View.dispatchTouchEvent(View.java:8476)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2400)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2093)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2372)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
at android.app.Dialog.dispatchTouchEvent(Dialog.java:765)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2333)
at android.view.View.dispatchPointerEvent(View.java:8671)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4171)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4037)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3587)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3640)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3606)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3723)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3614)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3780)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3587)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3640)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3606)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3614)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3587)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5855)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5829)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5800)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5945)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at androi 11-10 15:53:48.494 5741-5741/user.com.test2 D/AndroidRuntime:
Shutting down VM


Calling from adaptor:-

@Override
public void onBindViewHolder(final MedicineAdaptor.ViewHolder holder, final int position) {


holder.ReminderCardView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {

Log.i("On Long Pressed","Yes");
//Toast.makeText(context,Integer.toString(listMedicine.getId()),Toast.LENGTH_LONG).show();

//Creating the instance of PopupMenu

final PopupMenu popupMenu=new
PopupMenu(context,holder.ReminderCardView);

//Inflating the Popup using xml file
popupMenu.getMenuInflater().inflate(R.menu.reminder_options,popupMenu.getMenu());

//registering popup with OnMenuItemClickListener

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){


@Override
public boolean onMenuItemClick(MenuItem item) {

if (item.getTitle().equals("Edit Details"))
{


//Edit details
//((AddReminder)mactivity).openDialogToAddReminder();
Reminders reminders=new Reminders();
reminders.openDialogToAddReminder(context, dbHelper, listMedicine.getRemender_id(),listMedicine.getId());

update(listMedicine);

}

return false;
}
});

popupMenu.show(); //showing popup menu


return false;
}
});

}


MainActivity.java

public void cameraIntent(Context context)
{
Intent takingPictureCameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
**if (takingPictureCameraintent.resolveActivity(getPackageManager())!=null)**
startActivityForResult(takingPictureCameraintent, REQUEST_CAMERA);
}


logcat is showing the error at highlighted line above:-

Research done:- From what i able to figure out using google and stackoverfollow. It is because of context.

so I tried
context.getPackageManager()

but by doing this camera is not working in mainActivity also.

Edit:--

Mainactivity.java

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();}

}


Updated:-

public void cameraIntent(Context context)
{
Intent takingPictureCameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (context.getClass().getSimpleName().equals("ListReminders"))
{
context.**startActivityForResult()** // bolded region is red color. means no fuction found.
}
else
{
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);** // getting nullpointerexception
saveToGallery(thumbnail);
}
catch (Exception e){e.printStackTrace();}

}

Answer

give try to this.
since you are calling the cameraIntent from adapter class. and in this method the startActivtyForResult() is method of activity class. you need to call context.startActivtyForResult() instead of calling startActivtyForResult() when you are calling from adapter.

you can achieve it by adding a callType parameter in the openDialogToAddReminder

if(callType.equals("fromActivity"))
{
startActivtyForResult(....);
}
else
{

((Activity)context).startActivityForResult(); }