0

I know this is common to ask, but I'm confused about how can I pass the data bitmap Image back to the first activity,when I click the button from first Activity it will goes to the second activity, when the user done filling the signatories it will have the format of bitmap and pressing save will display the image bitmap back to the first Activity, how can I achieve this?, I tried the following code below but it doesn't display the image, need help

enter image description hereenter image description here

Currently I'm trying this code below to pass from second activity to first activity but it doesn't work

Bitmap bitmap = signaturePad.getSignatureBitmap();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("imageExample", bitmap.toString());
startActivity(intent);

First Activity

ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btnNext = (Button) findViewById(R.id.btnNext);

    image = findViewById(R.id.imgUri);

    //get image from second activity
    getImage();

    btnNext.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    });
}

    public void getImage(){
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String resultUri = extras.getString("imageExample");
        Uri myUri = Uri.parse(resultUri);
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),myUri);
            image.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 374, 500, false));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

SecondActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.accomplished_signature);

    SignaturePad signaturePad = findViewById(R.id.signature_pad);
    Button buttonSignature = findViewById(R.id.btnSubmit);
    Button buttonClear = findViewById(R.id.btnClear);
    buttonClear.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signaturePad.clear();
        }
    });

    buttonSignature.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bitmap bitmap = signaturePad.getSignatureBitmap();
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtra("imageExample", bitmap.toString());
            startActivity(intent);
            signaturePad.clear();
        }
    });

}
  • use `byteArray` to get back image from another to the first activity. see this https://stackoverflow.com/a/11519855/16765223 – M DEV Aug 10 '22 at 09:17

1 Answers1

0

you'r trying to convert a bitmap to a string which is not possible, instead do it like this :

Intent intent = new Intent(this, ActivityTwo.class); intent.putExtra("bitmap", bitmap);

in order to retreive it from your sencod activity :

Intent intent = getIntent(); Bitmap bitmap = (Bitmap) intent.getParcelableExtra("bitmap");

Gaya Touak
  • 56
  • 5