如何从自定义相机应用程序将照片传输到另一个活动

时间:2016-06-02 21:38:12

标签: android camera

我已经构建了一个自定义相机应用程序,我想要做的就是使用按钮捕获图像,然后立即在另一个活动中显示图像,我可以在其中添加过滤器和内容。我想知道如何捕获和传输图像。 到目前为止没有任何帮助。

    Button capture_image = (Button)findViewById(R.id.capture_button);
    capture_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCamera.takePicture(null, null, mPicture);
        }

    });
}


Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {


    }

};

那么,我应该添加什么" onPictureTaken"以及如何将图片调用到我的" PreviewActivity.class"

In" PreviewActivity.class"我已经有一个名为:imageView

的ImageView

1 个答案:

答案 0 :(得分:0)

将动作委托给其他应用程序的Android方式是调用描述您想要完成的内容的Intent。此过程涉及三个部分:Intent本身,启动外部Activity的调用,以及焦点返回活动时处理图像数据的一些代码。

这是一个调用捕捉照片意图的功能。

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}
String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            ...
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

参考:https://developer.android.com/training/camera/photobasics.html