I have my main Activity in which I have a few Buttons.
One of them is supposed to open the Camera.
To make it cleaner I am using View.onClick()
, so I will just have the Button on the main Activity and the rest will be managed by another other class (Camactivity).
In my main Activity :
Button btnrep = (Button)findViewById(R.id.button3);
btnrep.setOnClickListener(new Camactivity(this));
and in my
public class Camactivity extends Activity implements View.OnClickListener
private File imageFile;
private Context appContext;
public MainActivity_1(Context context)
{
appContext = context;
}
@Override
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);//use intent and pass in mediastore
//mediastore is a databases where image and video are stores and link
imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "BreedingGround.bmp");
/*link to a directory - pass in directory where you want to save the pictures and names of the file*/
Uri tempuri = Uri.fromFile(imageFile);//Convert imageFile to a Uri
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempuri);//location where u want the image file to be save after taking photo
intent.putExtra(android.provider.MediaStore.EXTRA_VIDEO_QUALITY, 1);//quality of out image, 1 means high quality image
startActivityForResult(intent, 0);//Request code 0 to identify who send the request
}
However I am getting a null pointer error at startActivityForResult(intent, 0);
FATAL EXCEPTION: main Process: com.example.mohit.softeng, PID: 22075
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:97)
at com.example.mohit.softeng.MainActivity_1.onClick(MainActivity_1.java:60)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10826)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)