简体   繁体   中英

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

Im using a camera application in android. I want to pass the byte data from PictureCallback method to another activity and want to display it in that activity.

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:

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:

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 .

EDIT: @ss1271's comment is right. According to this answer seems that there's a limit of 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.

Best wishes, Tim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM