Pass imageView to next Activity- Android - android

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);

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

Related

How would I take an image on one activity and move it into another activity

If I grid view of many images and the user selects one images and click next. How would I move the selected image from one screen into another screen. for example on the intro screen I have a image of a puppy and I click on that image that makes it go into another activity but how would I transfer that images into the second activity?
What I have tried doing is that I have tired doing it by the same principle of moving strings from one activity to another but no luck on that one.
Thank You,
Harsh
To write
byte[] imgByteArray = ... convert Bitmap to byte array
Intent I = new Intent(YourActivity.this, NewActivity.class);
i.putExtra("imgBytes", imgByteArray);
startActivity(i);
To read
Bundle extras = getExtras();
byte[] imgByteArray = extras.getByteArray("imgBytes");
Bitmap bitmap = ... convert byte array to Bitmap
Ideally it would be better to use some kind of image caching library like Picasso or Universal Image Loader for better memory handling and so you could pass image url instead of bytes.

Android - pass an image to other activity, resize and keep quality

I have a listview, getting news thumbnail and header from json.
I want to show news body and bigger image in other activity by clicking listview item. The code below send image to other activity.
secilenresim= (ImageView)view.findViewById(R.id.mansetresim);
secilenresim.buildDrawingCache();
Bitmap image= secilenresim.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
//// the code below gets the image in new activity
haberresim=(ImageView)findViewById(R.id.haberresim);
haberresim.getLayoutParams().height = 300;
haberresim.getLayoutParams().width = 400;
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
haberresim.setImageBitmap(bmp);
all works fine. but image quality in new activity is too bad. whereas image source (coming from json and loaded by picasso library) is fine, and the image resolution is 600*400 pixels. How can I pass image to other activity and keep quality?
If you are using picasso, your image is downloaded once and then kept in memory or even disc-cache. So you won't have to pass the bitmap via bundle but only the URL to it from your JSON.
In your Detail-Activity you can request the image via picasso again for your larger imageView.
You can check if your image is loaded from cache or network if you enable the picasso debug flag:
picasso.setDebugging(true)
getDrawingCache() may be getting image with low quality.
to change it use:
secilenresim.setDrawingCacheQuality(DRAWING_CACHE_QUALITY_HIGH);
you can check your quality with getDrawingCacheQuality(). It can be one of those:
DRAWING_CACHE_QUALITY_AUTO
DRAWING_CACHE_QUALITY_LOW
DRAWING_CACHE_QUALITY_HIGH
EDIT:
it seems that secilenresim.destroyDrawingCache(); before building it may be also helpful

Android save working bitmap

I have a Bitmap that I chose from gallery that is getting edited
like putting on it stickers, adjusting hue, brightness ..
putting colors etc..
It's a photo editing app...
It's all working fine, my last problem is
How do I make the Bitmap saves so it goes to the next activity
like,,
In my last activity where u share the image u made, there is an imageview, and it's working it shows the bitmap that u imported form the gallery but it's not editied
like it shows the original Bitmap
not the one I edited it..
How do I make when they click the button done in the editing activity the currentBitmap gets saved so in the share activity where done will take them it shows what they did
try this
//create a class in your package
class objectclass{
public static Bitmap bmp;
}
//save your previous activity bitmap like this
....
objectclass.bmp = b;//your bitmap
....
//use in another activity like this
Bitmap b1 = objectclass.bmp;
hope help

Android: Saving a Bitmap Graph to the Image Gallery Using OnClick Listener

I am a high school student looking for help saving a bitmap image creating using a modified version of the "Sensor Graph" tutorial available here: http://code.google.com/p/amarino/downloads/detail?name=SensorGraph_02.zip&can=2&q=
This creates an oscilloscope graph based on external sensor activity, and I need to save the image (currently a bitmap) as a JPEG to the Android phone Image Gallery. I would like to do so using a button.
I have enabled OnClickListener in my SensorGraph class, an extension of the Activity class; however, the actual bitmap is created in the View class.
I would appreciate if someone could provide some code to help me save the bitmap.
I can also use a general "OnClick" command in the main.xml file; however, I believe that the method specified there would just refer back to the Activity class, so I still do not know how to save a bitmap created in the View class using a method in the Activity class.
Thank you very much.
Please try following code,
ImageView v1 = (ImageView)findViewById(R.id.mImage);
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();

How to send a bitmap between two different activity classes in android

In our application we need to send a bitmap from one activity class to another activity after doing some image processing. We call methods in the first activity and then we want to show the output image in the second activity. The two activity classes have different layout xml files. How can we do that?
the Bitmap is parceable as EboMike said , so in your first Activity , you can do this :
Intent intent = new Intent(this,SecondActivity.class);
intent.putExtras("MYBITMAP",yourImage);
startActivity(intent);
and in your SecondActivity , add this code :
Bitmap imageToDisplay = (Bitmap) this.getIntent().getExtras("MYBITMAP");
//and then you can display it in your imageView :)
A Bitmap is parcelable, so you can send it as an extra, BUT this is a bad idea if your bitmap is big - it might fail on older phones that don't have much RAM.
If you have really big Bitmaps, you should consider writing them to the internal storage as they are being transferred. This will also handle the case where a user temporarily switches to a different app (like an incoming phone call) and then comes back to your app, which has possibly been terminated at that point.
If the activities are in the same apk then the best way is to simply to use a static variable.
You will be processing the bitmap object (from a Canvas?)
class Globals {
public static BitmapDrawable processedBitmapDrawable=null;
}
....
in process activity:
Bitmap processedBitmap = canvas.getBitmap();
Globals.processedBitmapDrawable = new BitmapDrawable(processedBitmap);
...
in second acitity:
if (Globals.processedBitmapDrawable!=null) {
imageView.setDrawable(Globals.processedBitmapDrawable);
}
it seems (and is) simple, but is the best way as it save processing/loading the bitmap multiple times.
You might choose to also use a SoftReference<Bitmaprawable> this allows garbage collection to clean up the reference if nessecary. though you may ned to reload/repocess if you need it again.

Categories

Resources