【问题标题】:How to pass a captured image from custom camera to another activity?如何将捕获的图像从自定义相机传递到另一个活动?
【发布时间】:2020-01-22 22:59:10
【问题描述】:

我目前正在尝试构建一个自定义相机应用程序,该应用程序将捕获图像,然后将其显示在另一个活动中(通过更新 ImageView)。

目前,我可以通过我的相机预览成功捕获并保存设备上的图像,甚至可以使用捕获的图像成功更新该活动中的 ImageView - 但是,我在通过捕获的图像到第二个活动。

我在网上看到很多结合使用 ActivityResult 和 Intent 的解决方案,但它们似乎都使用设备的本机相机应用程序,而不是像我这样使用自定义相机。

我的 CameraActivity 类如下所示:

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_activity);
    swatch = (ImageView) findViewById(R.id.swatch);
    preview = (FrameLayout) findViewById(R.id.camera_preview);
    cameraButton = (Button) findViewById(R.id.captureButton);


    cameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mCamera.takePicture(null, null, mPicture);

        }
    });
}

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

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null) {
            Log.d("Camera error",
                    "Error creating media file, check storage permissions: ");
            return;
        }
        Log.i("Picture", pictureFile.toString());
        try {

            final ImageView imageView = (ImageView) findViewById(R.id.imageView);
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            BitmapFactory.Options scalingOptions = new BitmapFactory.Options();
            scalingOptions.inSampleSize = camera.getParameters().getPictureSize().width / imageView.getMeasuredWidth();
            final Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, scalingOptions);

        } catch (FileNotFoundException e) {
            Log.d("Camera error", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("Camera error", "Error accessing file: " + e.getMessage());
        }

        finish();
        openColourDetectorActivity();
    }
};

private static File getOutputMediaFile(int type) {

    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
            "Medici");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.i("Medici", "failed to create directory");
            return null;
        }
    }

    @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyymmddhhmmss")
            .format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}


public void openColourDetectorActivity() {
    Intent intent = new Intent(this, ColourDetectorActivity.class);
    startActivity(intent);
}

当然,我试图在该活动的 onCreate 方法的另一端接收它。

知道我该怎么做吗?

【问题讨论】:

    标签: java android image camera imageview


    【解决方案1】:
    openColourDetectorActivity(pictureFile);
    
    public void openColourDetectorActivity(File file) {
        Intent intent = new Intent(this, ColourDetectorActivity.class);
        intent.putExtra("FILE", file.getPath());
        startActivity(intent);
    }
    

    在 ColourDetectorActivity 中:

    new File(getIntent().getStringExtra("FILE"))
    

    【讨论】:

    • 太棒了!这很好用。我已经将第二个活动上的文件转换为位图,并用它来更新 ImageView 并显示照片。所以一切都按预期工作。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多