Shubhendra
Shubhendra

Reputation: 117

how to pass the image from grid view to another activity in android

I have a grid view with images and on image click the particular image should be passed to the next activity.Now if working with one single image drawable this steps is easy but how to get results when working with images in grid view.Images are stored in client side. Here is my code for activity1 with grid view

public class GridActivity extends AppCompatActivity {

GridView androidGridView;

Integer[] imageIDs = {
        R.drawable.alien, R.drawable.capt_spaulding, R.drawable.captain,
        R.drawable.stark, R.drawable.casper, R.drawable.captainamerica,
        R.drawable.spidey, R.drawable.abra, R.drawable.bulbasaur
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grid);

    androidGridView = (GridView) findViewById(R.id.avatar_grid);
    androidGridView.setAdapter(new ImageAdapterGridView(this));

    androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.captain);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            Toast.makeText(getBaseContext(), "Grid Item " +
                    (position + 1) + " Selected", Toast.LENGTH_LONG).show();

            Intent intent = new Intent(GridActivity.this,FinalActivity.class);
            gosudo.putExtra("picture", byteArray);
            startActivity(intent);
        }
    });

}

public class ImageAdapterGridView extends BaseAdapter {
    private Context mContext;

    public ImageAdapterGridView(Context c) {
        mContext = c;
    }

    public int getCount() {
        return imageIDs.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView mImageView;

        if (convertView == null) {
            mImageView = new ImageView(mContext);
            mImageView.setLayoutParams(new GridView.LayoutParams(130, 130));
            mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mImageView.setPadding(16, 16, 16, 16);
        } else {
            mImageView = (ImageView) convertView;
        }
        mImageView.setImageResource(imageIDs[position]);
        return mImageView;
}
}
}

And this is finalActivity where the chosen image is shown

public class FinalActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_final);

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

    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
   //image view where the chosen image should set
    ImageView image = (ImageView) findViewById(R.id.selectedimageview);

    image.setImageBitmap(bmp);

}
}

I have been through questions like stackquestions but got no success.

Upvotes: 0

Views: 3468

Answers (3)

Shubhendra
Shubhendra

Reputation: 117

So here is the solution, GridActivity

 int imageRes = imageIDs[position];

            Intent intent = new Intent(GridActivity.this,FinalActivity.class);
            intent.putExtra("IMAGE_RES", imageRes);
            startActivity(intent);

In FinalAct

 ImageView image = (ImageView) findViewById(R.id.selectedimageview);

    Bundle extras = getIntent().getExtras();
    int imageRes = extras.getInt("IMAGE_RES");

    image.setImageResource(imageRes);

Upvotes: 0

Rajilesh Thayath
Rajilesh Thayath

Reputation: 3

@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

           //get image resource based on adapter click position.
           int imageRes = imageIDs[position];

           //then pass it with the intent.
           Intent intent = new Intent(GridActivity.this,FinalActivity.class);
           intent.putExtra("IMAGE_RES", imageRes);
           startActivity(intent);
        }
    });

    //In FinalActivity.class
    //retrieve the resource clicked and set to the imageView.
    int imageRes = getIntent().getExtra("IMAGE_RES");
    image.setImageResource(imageRes);

Upvotes: 0

Since you are using resource you can pass the id.

 Intent intent = new Intent(this, FinalActivity.class);
 intent.putExtra("imageId",imageId);

And in the final Activity:

 int imageId = getIntent().getIntExtra("imageId");
 image.setImageResource(imageId);

Upvotes: 1

Related Questions