How to return a bitmap from a child activity in android

I tried using the code below to return a bitmap from a child activity to a parent activity, and I get a null pointer exception,

Child Process 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?

+4
source share
2 answers

you should get a bitmap from 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);
        }
+5
source

Use datainstead getIntent()to get ByteArrayin onActivityResult:

byte[] byteArray = data.getByteArrayExtra("overlay");
+2
source

Source: https://habr.com/ru/post/1534203/


All Articles