How to pass image from one Imageview to an Imageview from another Activity?

Asked

Viewed 720 times

0

I made an app that takes the image from the gallery and puts it in a Imageview, So far so good. Now I’d like to take this image that’s on Imageview and move on to a Imageview other’s Activity.

I tried that:

Primeira Activity:

public void sertarImagemEmOutraTela (View v){

    Drawable drawable = imageView.getDrawable();
    BitMap bitmap = ((BitmapDrawable)drawable.getBitMap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    byte[] bytes = stream.toByteArray(); 

    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("bitmapbytes",bytes);
    startActivity(intent);


}

Segunda Activity:

    imageView = findViewById(R.id.imageView);


    Intent intent = getIntent();

    Bundle param = intent.getExtras();
    byte[] bytes = param.getByteArray("BITMAP");
    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    imageView.setImageBitMap(drawable);

Imagem do erro que aparece na segunda activity

  • What resulted from the attempt?

  • this giving error, the image is not shown in the second Activity.

  • What’s the mistake ?

  • i can bring the image of the gallery and set in the imageview of the first Activity, more when I try to move to the second screen of an error

  • What method did you use to assign the image to Imageview?

  • used Intent Intent = new Intent(Intent.ACTION_PICK);

  • together with onActivityResult

  • Bitmap bitmap = ((Bitmapdrawable)drawable.getBitMap(); Is this code snippet correct ? Bitmap != Bitmap

  • Yeah, this code is making a mistake.

  • What I wanted to ask was if you used imageView.setBackground() or used another method.

  • imageView.setImageBitMap(drawable);

Show 6 more comments

2 answers

1

As far as I know the identifier that you put in the Intent for bytes should be the same when it is to rescue your bytes in the second Activity.

Example, if you put "bitmapbytes" as identifier for your bytes in putExtras("bitmapbytes", bytes) you should use this same identifier to pick up the content, exchange "BITMAP" for "bitmapbytes" that the problem must be solved, and Nullpointerexception is occurring because you are using decodeByteArray(bytes, 0, bytes.length); with empty bytes.

  • Samuel your comment helped me a lot...!

1

Gentlemen, good morning, gentlemen! I thank the comments, I come here to inform that I managed to resolve my doubt.

follows below all the code, I’m sure it will help other people who are starting just like me.

first Activity:

public class MainActivity extends AppCompatActivity {

Uri image;
ImageView img;
Bitmap bitmap_ok;
Button btnImagem1,btnImagem2 ;

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

    btnImagem1 = findViewById(R.id.btnImagem1);
    btnImagem1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1);
        }
    });

    img = findViewById(R.id.imageView);

    btnImagem2 = findViewById(R.id.btnImagem2);
    btnImagem2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            passarImagem();
        }
    });
}

private void passarImagem() {
    img = findViewById(R.id.imageView);
    Drawable drawable = img.getDrawable();
    Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] imageInByte = stream.toByteArray();

    Intent intent = new Intent(MainActivity.this,SegundaTela.class);
    intent.putExtra("imagem",imageInByte);
    startActivity(intent);
    finish();
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK){
        image = data.getData();
        try {
            bitmap_ok = MediaStore.Images.Media.getBitmap(getContentResolver(),image);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap_ok.compress(Bitmap.CompressFormat.JPEG, 100, stream);

            img.setImageBitmap(bitmap_ok);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

}

second activy:

public class SegundaTela extends AppCompatActivity {
ImageView imgs;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_segunda);
     try {
         Bundle bundle = getIntent().getExtras();
         if (bundle != null){
             try {
                 byte[] imageInByte = bundle.getByteArray("imagem");
                 Bitmap bmp = BitmapFactory.decodeByteArray(imageInByte,0,imageInByte.length);
                 imgs = findViewById(R.id.imageView22);
                 imgs.setImageBitmap(bmp);
             }catch (Exception e){}
         }
     }catch (Exception e){
        Toast.makeText(this,""+e,Toast.LENGTH_SHORT).show();
        e.printStackTrace();
     }
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.