ace7410 ace7410 - 1 month ago 15
Java Question

Calling the camera from another Activity

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)

Answer

You are more than welcome to define a View.OnClickListener subclass, but there's really no reason I see to have Camactivity at all, so just implement View.OnClickListener onto MainActivity.

Besides, you will likely want the result from startActivityForResult within MainActivity's onActivityResult, not Camactivity.

So, with that suggestion

Button btnrep = (Button)findViewById(R.id.button3);
btnrep.setOnClickListener(this);

If you already have your Activity implementing an OnClickListener, then add an if-statement to check which button is clicked.

@Override
public void onClick(View view) {
    switch (v.getId()) {
        case R.id.button3:
            openCamera();
    }
}

private void openCamera() {
    // That other code in Camactivity
}


Alternative answer... still not using new to make an Activity class.

Button btnrep = (Button)findViewById(R.id.button3);
btnrep.setOnClickListener(new View.onClickListener() {
    @Override
    public void onClick(View v) {
        Intent cam = new Intent(MainActivity.this, Camactivity.class);
        startActivityForResult(cam, 0); // If you need the result in MainAcivity, pass it back from camActivity
        // else, just startActivity(cam);
    }
});

Then, update Camactivity to start the camera intent as soon as it is created.

public class Camactivity extends Activity {

    private File imageFile;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        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

    } 

    @Override
    public void onActivityResult (int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            // TODO: Handle camera intent result
        }
    }



}
Comments