简体   繁体   中英

How to store image captured using camera.takePicture(null,null,mPicture) so that it can be displayed it in next activity?

I am making a custom camera application in android. I capture the image using

camera.takePicture(null,null,mPicture)" and "Camera.PictureCallBack()

I want to store the image taken from the camera so that I can display it in the next activity . How can it be done?

    Camera.PictureCallback mPicture = new Camera.PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        File pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);

            fos.close();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
        }
    }
};



private static File getOutputMediaFile() {
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "MyCameraApp");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
}

Pass uri in extras of your next activity intent

public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE_USING_CAMERA) {
            try {
               Intent i = new Intent(FirstActivity.this, SecondActivity.class);
               i.putExtra("uri",outputFileUri);
               startActivity(i);
            } catch (Exception ex) {
                Log.e("Exception", ex.toString());
            }
        }
    }
}

then retrieve image in your second activity

Uri = getIntent().getExtras("uri");

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;

final Bitmap capturedimage = BitmapFactory.decodeFile(uri, options);
imgPreview.setImageBitmap(capturedimage );

TO open Camera

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, SELECT_PICTURE_USING_CAMERA);

Then need to implement onActivityResult to get the data.

public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE_USING_CAMERA) {
            try {
               final File file = new File(outputFileUri.getPath());
            } catch (Exception ex) {
                Log.e("Exception", ex.toString());
            }
        }
    }
}

Then you can take the image form the file path to display in the next activity.

You can use CameraView to implement the custom camera in very simple way which gives you the functionalities to capture image and video. You can see the DEMO where it the CameraActivity tooks the picture and PicturePreviewActivity and VideoPreviewActivity are use to preview the picture and video taken in CameraActivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM