Question

I know this has been asked here, but my problem is a bit different. I read other answers related to this topic on SO but they did not solve my problem.

So in my app, I have 2 activities. In the first one, there are 20 imageviews (and I am planning to add more) and in the second activity, there is a custom view which is actually a zoom image class. Basically, when the user will click on any imageview in the first acivity he will be taken to the 2nd activity where he can zoom that particular image.

What I want to do in the coding part is that get the image that is displayed on the clicked imageview and pass it to the next activity. To achieve that, I have used the below given code but it's not working. What could be wrong?

Activity_one

Intent startzoomactivity = new Intent(Activity_one.this, Activity_two.class);
Bitmap img_res = img.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", img_res);
startzoomactivity.putExtras(extras);
startActivity(startzoomactivity);

Activity_two

Intent startzoomactivity = getIntent();
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");

TouchImageView img = new TouchImageView(this); // TouchImageView is the class that enables zoom feature on the imageview.
img.setImageResource(i);
img.setMaxZoom(5f);

No correct solution

OTHER TIPS

you cannot pass the ImageView to the other activity. Views are directly tied to the activity they belong and cannot be passed around.

What you're trying to achieve in the code is to get a drawing cache copy of the ImageView as a bitmap, which even after you make it work (it's missing the view.setDrawingCache), it will give you a very bad result, specially after zooming-in.

What you need to do is find the original image (if it came from the internet the URL, or the location in your disk cache, if it's from your resources folder, the int resId that draw that image, and pass this value (string or int) to the next activity. Then you let the next activity create its own ZoomableImageView and set the original resource (link, file, resource) to it.

I understand what you want to achieve but your way is totally wrong. Instead of passing ImageView Bitmap pass the ImageView URL or Path (and if drawable then ResId) where this image coming from and pass that URL or Path or ResId to the Next Activity and load this image as a Bitmap onto ZoomableImageView.

You can get the image from imageview as a bitmap, then compress it using http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream) api. then covert the outputstream object to byte array and since that is parcelable you can put this object in an intent and pass to your second activity

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