Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return bitmap from child activity in android

I have tried below code to return bitmap from child activity to parent activity and I am getting null pointer exception,

Child Activity Code:

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
    overlayImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    Intent intent=new Intent();
    intent.putExtra("overlay",byteArray);
    setResult(RESULT_OK, intent);

    finish();

Parent Activity Code:

            protected void onActivityResult(int requestCode, int resultCode, Intent data)           {
                if (resultCode == RESULT_OK) {
            editorBitmapArray.add(current_bmp);
            byte[] byteArray = getIntent().getByteArrayExtra("overlay");
            current_bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            Image.setImage(current_bmp);
        }

How to return byte array from child activity to parent activity in android?

like image 732
pavan Avatar asked Mar 31 '14 07:03

pavan


2 Answers

you should get bitmap by data not getIntent()

   protected void onActivityResult(int requestCode, int resultCode, Intent data)           {
                if (resultCode == RESULT_OK) {
            editorBitmapArray.add(current_bmp);
            byte[] byteArray = data.getByteArrayExtra("overlay");
            current_bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            Image.setImage(current_bmp);
        }
like image 173
Ravi Kant Avatar answered Nov 03 '22 07:11

Ravi Kant


Use data instead of getIntent() to get ByteArray in onActivityResult :

byte[] byteArray = data.getByteArrayExtra("overlay");
like image 24
ρяσѕρєя K Avatar answered Nov 03 '22 08:11

ρяσѕρєя K