Nima
Nima

Reputation: 1

Send images from an activity and get them in another activity

i have Activity1 with 3 buttons. btn1 and 2 and 3. and have Activity2 with 3 imageViews, imageView1 and 2 and 3. i want when i click btn1 in act1, send an image to imageView1 in act2 (visible) and when click btn1 again,image be disappear(invisible) in act2. and when i click btn2 in act2, send an image to imageView2 in act2 (visible) and when click btn2 again,image be disappear(invisible) in act2. And btn3 also. and i whant save the changes and dont miss the changes when i exit the App. how can i do that?

Upvotes: 0

Views: 50

Answers (2)

Muntaser Ahmed
Muntaser Ahmed

Reputation: 4667

You can use intents to pass information between two activities. You can either:

  1. Convert the Bitmap of the image into a byte[] in the sending activity, and then pass that to the receiving activity OR
  2. Pass the file location from the sending activity to the receiving activity

Note that the former method is more expensive as you are converting the image to a Bitmap, then to a byte[], and then passing a "large" amount of information between two activities. In the latter method, you are merely passing a location and rendering the resource.

An example of method 1:

In the sending activity:

// Let image_name be the name of the image file located in your drawable folder
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_name);     
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray();

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);

In the receiving activity:

Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView imageView = (ImageView) findViewById(R.id.image_view_1);

You can then render the image in your ImageView by putting the following line in your button's OnClickListener.

imageView.setImageBitmap(bmp);

An example of method 2:

In the sending activity:

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picname", fileName);

In the receiving activity:

Bundle extras = getIntent().getExtras();
String fileName = extras.getString("picname");

File filePath = getFileStreamPath(fileName);
Drawable d = Drawable.createFromPath(filePath.toString());

You would then set your ImageView drawable by putting the following line in your button's OnClickListener.

someImageView.setBackgroundDrawable(d);

Regarding your other questions:

You can hide/show your image by manipulating the visibility of your ImageView.

imageView.setVisibility(View.VISIBLE);
imageView.setVisibility(View.INVISIBLE);

You can also save the state of your images by using a Bundle called savedInstanceState. Make sure to update the state (visibility) of your images when you toggle them, and in your onCreate method.

Upvotes: 0

SkyTyper
SkyTyper

Reputation: 54

Don't send the image from act1 to act2, just put them already in act2. Then when you click a button in act1, send the tag of the button pressed to act2 via putExtra() as follows:

public void clickButton1(View view) {
    Intent intent = new Intent(this, act2.class);
    intent.putExtra("bttnTag", 1);
    startActivity(intent);
}

Then you do the same for the other two buttons, sending 2 and 3 as the corresponding tags. And then on act2 you get which button's been clicked as follows:

    Intent intent = getIntent();
    int btnClicked = intent.get("bttnTag", -1);

And then you can use that btnClicked value to choose which of the three ImageViews you have to make visible/invisible. Hope it helps.

Upvotes: 1

Related Questions