How to launch the camera and take a picture

后端 未结 3 963
粉色の甜心
粉色の甜心 2020-12-18 17:07

I\'m trying to make an Android demo. In the demo, I have to show the camera in an activity and take a picture before advancing to another activity where I can see the camera

3条回答
  •  情书的邮戳
    2020-12-18 18:05

    In my app I use the following code to Launch the camera:

    public void imageFromCamera() {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        mImageFile = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApp",  
                "PIC"+System.currentTimeMillis()+".jpg");
        mSelectedImagePath = mImageFile.getAbsolutePath();
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mImageFile));
        startActivityForResult(intent, TAKE_PICTURE);
    }
    

    This will save the image to the path mSelectedImagePath which is /sdcard/MyApp/.jpg.

    Then you capture the return of the IMAGE_CAPTURE intent in onActivityResult and launch your activity to edit the image from there!

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch(requestCode) {
            case TAKE_PICTURE:
                        //Launch ImageEdit Activity
                Intent i = new Intent(this, ImageEdit.class);
                        i.putString("imgPath", "mSelectedImagePath");
                        startActivity(i);
                break;
            }
        }
    }
    

    Hope this helps!

提交回复
热议问题