Issue
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.
Solution
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);
Answered By - Shubhendra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.