Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/android cannot transfer byte array between activities

I'm trying to make an take photo application.

there are surfaceview as a preview and button "Take photo" on the main activity. on the second activity there are textView for some picture info and imageView for displaying picture;

I tried to trasfer data via putExtra() methods.

on the code in mainActivity

public void onPictureTaken(byte[] data, Camera camera) 
{
    // TODO Auto-generated method stub

    RecognizedActivity = new Intent(MainActivity.this, recognized.class);

    RecognizedActivity.putExtra("score", rFace.score);

    //RecognizedActivity.putExtra("leyex", rFace.leftEye.x);
    //RecognizedActivity.putExtra("leyex", rFace.leftEye.x);
    /*RecognizedActivity.putExtra("leye_y", rFace.leftEye.y);
    RecognizedActivity.putExtra("reye_x", rFace.rightEye.x);
    RecognizedActivity.putExtra("reye_y", rFace.rightEye.y);

    RecognizedActivity.putExtra("mouth_x", rFace.mouth.x);
    RecognizedActivity.putExtra("mouth_y", rFace.mouth.y);
    */
    RecognizedActivity.putExtra("rect_bottom"   , rFace.rect.bottom);
    RecognizedActivity.putExtra("rect_right"    , rFace.rect.right);
    RecognizedActivity.putExtra("rect_left"     , rFace.rect.left);
    RecognizedActivity.putExtra("rect_top"      , rFace.rect.top);

    //RecognizedActivity.putExtra("picture", data);
    RecognizedActivity.putExtra("pic", data);

    startActivity(RecognizedActivity);

    cam.startPreview();
}

on the recieving activity

public class recognized extends Activity 
{
int score;
int leye_x, leye_y;
int reye_x, reye_y;

int mouth_x, mouth_y;

int r_bottom, r_top, r_left, r_right;

byte[] picture;
TextView tvFaceInfo;
ImageView ivFaceDisplay;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recognized);

    Intent i = getIntent();
    tvFaceInfo = (TextView) findViewById(R.id.tvFaceInfo);
    ivFaceDisplay = (ImageView) findViewById(R.id.ivFacePicture);

    score       = i.getIntExtra("score", -1);

    r_bottom    = i.getIntExtra("rect_bottom"   , -1);
    r_right     = i.getIntExtra("rect_right"    , -1);
    r_left      = i.getIntExtra("rect_left"     , -1);
    r_top       = i.getIntExtra("rect_top"      , -1);

    picture     = i.getByteArrayExtra("pic");// getByteArrayExtra("picture");

    Bitmap bm = BitmapFactory.decodeByteArray(picture, 0, 1280*960);        
    //bm.copyPixelsFromBuffer(picture);
    ivFaceDisplay.setImageBitmap(bm);
    tvFaceInfo.setText("score: " + score + "\n"
                        + "rect " + r_bottom + " " + r_right + " " + r_left + " " + r_top);
}
}

when the debuger gets on picture = i.getByteArrayExtra("pic"); exception is threw

"Source not found"

what's wrong ?

like image 372
theroom101 Avatar asked Aug 06 '26 05:08

theroom101


1 Answers

You can pass limited amount of data between processes (activities in your case). Instead of passing byte array itself, save your byte array (image) as file and pass a path to that file instead (URI for ContentProvider for example).

like image 57
Dmitry Zaytsev Avatar answered Aug 07 '26 17:08

Dmitry Zaytsev