如何存储使用camera.takePicture(null,null,mPicture)捕获的图像,以便可以在下一个活动中显示它?

时间:2017-12-07 13:12:37

标签: android android-camera

我在android中制作自定义camera应用程序。我使用

捕获image

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

我想存储从image中获取的camera,以便我可以在下一个activity中显示它。怎么办呢?

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

3 个答案:

答案 0 :(得分:2)

将uri传递给你下一个活动意图的额外内容

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

然后在第二个活动中检索图像

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

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

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

答案 1 :(得分:1)

打开相机

allprojects {
    repositories {
        jcenter()
        google()
        maven { url 'https://jitpack.io' }
    }
}

然后需要实现onActivityResult来获取数据。

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

然后,您可以从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()); } } } } 路径中获取图像,以便在下一个活动中显示。

答案 2 :(得分:0)

您可以使用CameraView以非常简单的方式实现自定义相机,从而为您提供捕获图像和视频的功能。您可以看到DEMO拍摄照片的CameraActivityPicturePreviewActivityVideoPreviewActivity用于预览在CameraActivity中拍摄的照片和视频。