3

I am using two activities. One activity displays images in a GridView and by clicking on a particular image in that GridView it should display the full screen image in another activity.

How can I achieve this?

My MyGridView.java

mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "Image"+(position+1),Toast.LENGTH_SHORT).show();
    System.out.println(id);
    Intent i = new Intent(this, MyImageViewActivity.class);
    Bundle bundle = new Bundle();
    bundle.putInt("image", position);
    i.putExtras(bundle);
    startActivityForResult(i, 0);
}
});
Saurabh
  • 457
  • 2
  • 8
  • 26

9 Answers9

3

Pass the image URL/Uri instead of passing raw image data.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
2

You pass parameters to an Activity in an Intent. If the image comes from a file, pass the path String, otherwise pass the Bitmap

startActivity(new Intent(this, YourActivity.class).putExtras(new Bundle().putParcelable("bitmap", Bitmap)))
P Varga
  • 19,174
  • 12
  • 70
  • 108
2

To pass the data between two activities:

bytes[] imgs = ... // your image
Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("img", imgs);
startActivity(intent);

Then in YourActivity:

bytes[] receiver = getIntent().getExtra("imgs");

Also go thro this link which wil also help u.
Here u can know how to convert bitmap to bytes

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
Hussain
  • 5,552
  • 4
  • 40
  • 50
2

In MyGridView: (someInteger is an integer that represents the index of the selected image

Intent myIntent = new Intent(this, MyImageViewActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("image", someInteger);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);

In MyImageViewActivity:

Bundle bundle = this.getIntent().getExtras();
int pic = bundle.getInt("image");

of course, you can put anything in the bundle! maybe a byte array or something

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
1

Once an item of Grid View is clicked, get the clicked item and pass it to next activity as an argument through PutExtra. In the next activity retrieve the image from extras and display it to user

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
1

I suppose you need use Intent class.

Intent intent = new Intent(YourSourceActivity.this, TargetActivty.class);
Bundle addinfo = new Bundle();

addinfo.putInt("imageid", someid);

intent.putExtras(addinfo);
movax
  • 352
  • 1
  • 8
  • Thanks man and plus In the target activity we just need Bundle bundle = this.getIntent().getExtras(); int pic = bundle.getInt("imageid"); – ashim888 Jan 01 '16 at 16:20
0

This is my process: it's so good. Activity1:

ByteArrayOutputStream stream=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
    byte[] byteArray=stream.toByteArray();
    Intent intent = new Intent(getApplicationContext(), FrameActivity.class);
    intent.putExtra("Image", byteArray);
    startActivity(intent);

in FrameActivity.class

collageView = (CollageView) findViewById(R.id.btn_collage);
    byte[] byteArray = getIntent().getByteArrayExtra("Image");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    collageView.setImageBitmap(bmp);
0

Try passing id related to image through intent.putExtra(), and receive it through bundle on launched activity.

MGK
  • 7,050
  • 6
  • 34
  • 41
0

in Activity convert the image to ByteArray and append it to the intent as

intent.putExtra("img",<ByteArray>);

then startActivity B.

In Activity B

Bitmap bm = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("img"), 0, getIntent().getByteArrayExtra("img").length);

This way you can pass image between activity.

Jana
  • 2,890
  • 5
  • 35
  • 45