简体   繁体   English

如何将相机捕获的图像传递到android中的下一个屏幕?

[英]How to pass image captured by camera to next screen in android?

Im using a camera application in android. 我在android中使用相机应用程序。 I want to pass the byte data from PictureCallback method to another activity and want to display it in that activity. 我想将字节数据从PictureCallback方法传递给另一个活动,并希望在该活动中显示它。

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


        }
    };

If anyone Knows about it, please help me.. 如果有人知道,请帮助我。

You can do that with extras: 您可以使用其他功能:

Camera.PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Intent i = new Intent(context, B.class);

            Bundle bundle = new Bundle();
            bundle.putByteArray("photo", data);
            i.putExtra(bundle );
            startActivity(i);
        }
};

and the on the B Activity: 和B活动:

Bundle extras = getIntent().getExtras();
byte[] photo = extras.getByteArray("photo");

To show the image on the second activity, you must convert the byte[] into a bitmap and the assign it to a imageView: 要在第二个活动中显示图像,必须将byte []转换为位图并将其分配给imageView:

Bitmap bitmap  = decodeByteArray (photo, 0, photo.length);
ImageView imgView = (ImageView)findViewById(R.id.preview);
imgView.setImageBitmap(bitmap);

I never tried to decode from byte[] to bitmap.. but you can find more information here . 我从没有尝试过从byte []解码为位图..但是您可以在这里找到更多信息。

EDIT: @ss1271's comment is right. 编辑: @ ss1271的评论是正确的。 According to this answer seems that there's a limit of 500Kb. 根据此答案,似乎有500Kb的限制。 which means that if your image is big you should save it and pass the reference to the new activity like this: 这意味着,如果您的图片很大,则应保存它,并将引用传递给新活动,如下所示:

// A ACTIVITY

Camera.PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            String fileName = "tempIMG.png";
            try {
                FileOutputStream fileOutStream = openFileOutput(fileName, MODE_PRIVATE);
                fileOutStream.write(data);
                fileOutStream.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            Intent i = new Intent(context, B.class);

            Bundle bundle = new Bundle();
            bundle.putExtra("photoPath", fileName);
            i.putExtra(bundle);
            startActivity(i);
        }
};

// B ACTIVITY

Bundle extras = getIntent().getExtras();
String photoPath = extras.getString("photoPath");
File filePath = getFileStreamPath(photoPath);
//And do whatever you want to do with the File

You could add the bytes (data) to an Intent via putExtra(String name, byte value) and start a new Activity with that Intent. 您可以通过putExtra(String name,byte value)将字节(数据)添加到Intent,然后使用该Intent启动新的Activity。

Best wishes, Tim 蒂姆,祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将相机捕获的图像显示到android中的下一个屏幕 - How to display image captured by camera to next screen in android 摄像头拍摄的图像应转发到android中的下一个屏幕 - image captured by camera should be forwarded to next screen in android 如何将捕获的/画廊图像传递给android中的下一个活动 - how to pass captured/gallery image to next activity in android 如何通过相机将捕获的图像发送到下一个视图 - how to send captured image by camera to a next view 如何在 android 中保存从相机拍摄的图像 - How to save captured image from camera in android Android将捕获的图像保存为Android相机捕获的图像 - Android Saving Captured Image As Android Camera Captured 如何将捕获的图像从自定义相机传递到另一个活动? - How to pass a captured image from custom camera to another activity? 如何使用 Camera2 API android 提高捕获的图像分辨率? - How to improve captured image resolution with Camera2 API android? 如何识别从android中的相机捕获的图像的背景色? - how to identify the background color of image captured from camera in android? 如何获取Android相机捕获的最后一张图像的路径? - How to get the path to the last image captured by Android camera?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM