0

I am new at programming. I am implementing a project which consists of 3 java classes (FirstActivity, SecondActivity and ResultActivity). The image taken is at FirstActivity. I need to transfer the image to the SecondActivity and then from SecondActivity to ResultActivity. Take note that FirstActivity and the ResultActivity only has imageview to display with. Please help me. Can someone suggests a sample code for me to be guided. Any advice will be much appreciated :)

First Activity:

  ImageView imgTaken = findViewById(R.id.imgTaken);
            Bitmap bitmap = ((BitmapDrawable) imgTaken.getDrawable()).getBitmap();
            Intent i = new Intent();
            i.setClass(ProcessImage.this, Match.class);
            i.putExtra("Bitmap", bitmap);
            startActivity(i);

Second Activity:

final Bitmap bitmap  = (Bitmap) this.getIntent().getParcelableExtra("Bitmap");
Intent i = new Intent();
            i.setClass(Match.this, Request.class);
            i.putExtra("Bitmap", bitmap);
            startActivity(i);

Result Activity:

ImageView imageView = findViewById(R.id.imageView);

Bitmap bitmap  = (Bitmap) this.getIntent().getParcelableExtra("Bitmap");
    imageView.setImageBitmap(bitmap);

Please help me.

karan
  • 8,637
  • 3
  • 41
  • 78
student76
  • 21
  • 3

5 Answers5

2

Send with Intent like this following:

ByteArrayOutputStream bao = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);

byte [] ba = bao.toByteArray();

Intent i = new Intent(context,SecondClass.class);

i.putExtra("bitmap",ba);

Get the data from Intent like this:

byte [] ba1 =getIntent().getByteArrayExtra("bitmap");

Bitmap bitmap = BitmapFactory.decodeByteArray(ba1,0,ba1.length);
ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
0

Welcome to Stackoverflow, Simplest answer would be make your Bitmap static like this:

public static Bitmap bitmap = null;
bitmap = ((Bitmap) imgTaken.getDrawable()).getBitmap();

start next activity and then access that image like :

imageView.setImageBitmap(FirstActivity.bitmap);
Faisal
  • 705
  • 3
  • 16
0

Instead of using this

final Bitmap bitmap  = (Bitmap) this.getIntent().getParcelableExtra("Bitmap");

Make this change

 Bitmap bitmap = (Bitmap) getIntent().getExtras().get("Bitmap");
0

Simplest will be

  1. create a BaseActivity that extends all your first, second and result activity with BaseActivity.
  2. Store the image bitmap in a variable that is declare in BaseActivity.
  3. And from the BaseActivity you can have access to bitmap image from any of your Activity.

this one is the simplest way, but there are may other ways to accomplish this.

Suryakant
  • 172
  • 6
0

@student76 I don't know How you were doing this, but ill explain it more..

  1. create a base activity class ex: public class BaseActivity extends AppCompatActivity { } and crate doo all the works here

  2. create your first activity ex ex: public class FirstActivity extends BaseActivity { } in this you will have you will process the image and store the image on a variable that is declare in the base activity

  3. create your Second activity ex ex: public class SecondActivity extends BaseActivity { } now directly access the variable that you declare in BaseActivity. now you have access to the image
  4. create your Second activity ex ex: public class ThirdActivity extends BaseActivity { } Same as SecondActivity. in this way you can have access to the bitmap in all you activity.
Suryakant
  • 172
  • 6