在应用程序本身拍照后使用byte[]



我拍了一张照片,想把它放在另一个活动的ImageView中,但是出了问题。下面是我的代码:

Camera mCamera;
//Some Code
public void onClick(View v) {
    mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);        
}
Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
    public void onShutter() {           
    }
};
Camera.PictureCallback rawCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
        Intent inCapture = new Intent(MainActivity.this, SecondActivity.class);
        //Actually, I don't know where to put it: here or in jpegCallback
        inCapture.putExtra("captured", data);
        startActivity(inCapture);
    }
};
Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        //My attempt to load the result into ImageView in the same Activity - didn't work too
        if (data != null) {
            ByteArrayInputStream imageStream = new ByteArrayInputStream(
                    data);
            Bitmap theImage = BitmapFactory
                    .decodeStream(imageStream);
            imageView.setBackground(null);
            imageView.setBackground(new BitmapDrawable(
                    getResources(), theImage));
        }
    }
};

它没有运行SecondActivity,我不知道如何在onClick()中声明我的意图,并能够将byte[]数据放在其中(不同的类,我不能使意图静态-至少,我不能)。是否有更好的方法来捕捉照片,并将字节[]传递给另一个活动?

注:我知道还有Camera2是较新的,但到目前为止我还不知道如何使用它

你可以用ContentProvider,它有时可能被证明是有用的,虽然有点实现需要自己处理,但它并不坏。你能做的另一件事是把Bitmap对象放在一个queue或hashmap在一个单例类中并通过intent传递那个Bitmap的id,被调用的activity会取id并从那里提取它。您需要处理一些同步。

相关内容

最新更新