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();
}
}
}
What resulted from the attempt?
– ramaral
this giving error, the image is not shown in the second Activity.
– Thed Santana
What’s the mistake ?
– ramaral
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
– Thed Santana
What method did you use to assign the image to Imageview?
– ramaral
used Intent Intent = new Intent(Intent.ACTION_PICK);
– Thed Santana
together with onActivityResult
– Thed Santana
Bitmap bitmap = ((Bitmapdrawable)drawable.getBitMap(); Is this code snippet correct ? Bitmap != Bitmap
– Felipe Campos
Yeah, this code is making a mistake.
– Thed Santana
What I wanted to ask was if you used
imageView.setBackground()
or used another method.– ramaral
imageView.setImageBitMap(drawable);
– Thed Santana