2

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..

Binesh
  • 117
  • 3
  • 15

2 Answers2

4

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
Community
  • 1
  • 1
SERPRO
  • 10,015
  • 8
  • 46
  • 63
  • Hi SeRPRo, thanks for your reply. could u please help me to display it in that activity B, as image? – Binesh Nov 07 '11 at 10:38
  • I would not suggest pass the picture itself directly via bundle. The picture taken by camera can easily excess the size limit of bundle which will cause error like `ERROR/JavaBinder(7881): !!! FAILED BINDER TRANSACTION !!!`. Its better if u launch an AsyncTask to save the pic first then retrieve later. – dumbfingers Oct 10 '12 at 08:51
  • @ss1271 Thanks. I didn't thought about that. I added another solution more efficient :) – SERPRO Oct 10 '12 at 09:29
  • @SERPRO u r welcome bro. :-) Just mention that u might need to tell the asker don't forget to clear the /data file after use or it will use up the internal memory. – dumbfingers Oct 10 '12 at 09:32
  • @ssz1271 that's why I added "do whatever you want to do with the File" because I don't know if he will only show the image in the second activity or he will save it on the mobile. :) – SERPRO Oct 10 '12 at 09:34
0

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

Tim
  • 6,692
  • 2
  • 25
  • 30