How to pass image from an ImageView to an ImageView of another Activity?

0

I made an app that takes the image from the gallery and puts it in an ImageView , so far so good. Now I wanted to get this image in ImageView and move to an ImageView of another Activity .

I tried this:

First 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);


}

Second 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);

    
asked by anonymous 15.05.2018 / 20:41

2 answers

0

As far as I know the identifier you put in the intent for the bytes should be the same as when you are going to retrieve your bytes in the second activity.

Example, if you put "bitmapbytes" as an identifier for your bytes in putExtras("bitmapbytes", bytes) you should use this same identifier to get the content, change "BITMAP" to "bitmapbytes" that the problem should resolve, and the NullPointerException is since you are using decodeByteArray(bytes, 0, bytes.length); with empty bytes.

    
17.05.2018 / 12:14
0

Sirs, good morning! I thank you for the comments, I came here to inform you that I have been able to resolve my doubts.

The code below follows, I'm sure it will help others who are starting out 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();
     }
}

}

    
18.05.2018 / 17:11