Question

I have a grid view when i click an item inside the grid view and its going to another activity and view it there my code something like this :

gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub
             Intent i = new Intent(getApplicationContext(), MainActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
        }
});

Activity which is im going to view

Intent i = getIntent(); 
// Selected image id
int position = i.getExtras().getInt("s");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(imageAdapter.moodpic[position]);
Was it helpful?

Solution

You try to get Extra with wrong key here getInt("s").

Try this

int position = getIntent().getIntExtra("id",0);

OTHER TIPS

you should change with

int position = i.getExtras().getInt("id" , 0);

instead this

int position = i.getExtras().getInt("s");

This is because you have put "id" as a key so whenever you are retrieving then that key must be same and by default the value of int is 0.

I would highly recommend a different approach. It's possible if you REALLY want to do it, but it costs a lot of memory and is also slow. It might not work if you have an older phone and a big bitmap. You could just pass it as an extra, for example

intent.putExtra("data", bitmap)

A Bitmap implements Parcelable, so you can put it in an extra. Likewise, a bundle has putParcelable f you want to pass it inbetween activities, I would store it in a file. That's more efficient, and less work for you. You can create private files in your data folder using MODE_PRIVATE that are not accessible to any other app.

First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.

Convert Bitmap to Byte Array:-

   Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//    Bitmap bmp = BitmapFactory.decodeFile(path); You can use this also.
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

Pass byte array into intent:-

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

Get Byte Array from Bundle and Convert into Bitmap Image:-

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

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);

2) First Save image into SDCard and in next activity set this image into ImageView.

3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top