How to show image preview from GridView Activity in Android
I made a App with a GridView using this tutorial for android. I placed all the images into the drawable-hdpi folder. The gridview is working just fine but now i want to start or open another activity when the user touches an image in the gridview. The new activity should be a preview of that single image.
How can I do this?
This how i putted but its not working my 1activity :
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent fullScreenIntent = new Intent(v.getContext(), 2activity.class);
fullScreenIntent.putExtra(1activity.class.getName(),imageIDs);
1activity.this.startActivity(fullScreenIntent);
}
});
my 2activity :
public void onCreate(Bundle savedInstanceState, int[] imageIDs) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
Intent intent = getIntent();
long imageIDs = (Long) intent.getExtras().get(2activity.class.getName(开发者_如何学C));
ImageView imageView = (ImageView)v.findViewById(R.id.preview);
imageView.setLayoutParams( new ViewGroup.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT));
imageView.setImageResource((int) imageIDs);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
when i go to 2activity from 1activity its just showing nothing just blank its not catching images from 1st activity ???? what shall i do ???
Error here:
long imageIDs = (Long) intent.getExtras().get(2activity.class.getName());
wrong key, fix it by:
long imageIDs = (Long) intent.getExtras().get(1activity.class.getName());
Create an OnItemClickListener ( http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html ) that launches your activity and use the setOnItemClickListener method (http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener%29) to attach the listener to your GridView.
-Kurtis
In your 2Activity you are getting null value for
long imageIDs = (Long) intent.getExtras().get(2activity.class.getName());
you should put here:
int imageIDs1 = intent.getIntExtra(2activity.class.getName(), R.drawable.icon);
On Image click Get Image Bitmap, Convert bitmap to Bytes and pass bytes via intent to your activity
Bitmap b=image.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG,75, stream);
byte[] bytes = stream.toByteArray();
Intent i = new Intent(mContext, ShowImageActivity.class);
i.putExtra("Bitmap", bytes);
startActivity(i);
In 2nd Activity Get Bytes from bundle and set to imageview
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if(bundle.containsKey("Bitmap")) {
byte[] bytes = bundle.getByteArray("Bitmap");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
BitmapDrawable ob = new BitmapDrawable(getResources(), bmp);
photo.setBackground(ob);
}
}
精彩评论