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.
Related
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.
so after a lot of investigation, I finally made my app able to resume state (I simply just modified the OnCreate method in the MainAcitivity.java to load a "restore" page on activity kill if there is a saved bundle instance)
I believe once I fix this final problem, all will be good and I can finally sleep.
HOW do I get the Activity/Intent result from a Camera in PhoneGap once the app has been killed off due to the Background Processes limit or the possibility of "Do not keep activities being checked" (I have a surprising amount of users who have these restrictions enabled)
I thought it might be possible to make the camera save the file in a temp directory and then I just pass the URI through javascript as a hash url (so it'd be something like file:///android_www/index-restore.html#URI_TO_IMAGE)
But my only issue is - How do I even begin this in PhoneGap? I know what to do for everthing bar the temp storage of the image and retrieving the location through onCreate
Ok finally managed it, wrote a hacky solution but it works.
I modified the CordovaInterface and CordovaActivity by adding a function called "getSharedPref" which returns a shared preference that can be accessed throughout the app.
I modified Camera Launcher to force the stored name to be temp.jpg or temp.png depending on what ever input, then store it within the preference.
On the MainActivity.java, I use this.getSharedPref() if a bundle instance is not null, and then check for the key. Unfortunately the only way I could assign the variable in Cordova was by doing
super.loadUrl("javascript: var global_image = '" + file + "'");
then I did the usual routines to add the file in my program and it all work so far! Happy days
Down the track I will probably write a plugin to use, using super.loadUrl("file:///blablabla#" + file); didn't seem to work.
The only issues so far are that Images aren't resized and rotated correctly, and I still need to implement this for the PhotoGallery but so far so good.
edit:
I've managed to get the photos resized and re-orientated :) Next issue is implementing it onto the Photo Gallery now - intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, file); doesn't seem to apply for some reason.
edit:
Latest update - so it turns out AFTER the App has seemingly "crashed", you still get the Intent back regardless, so modifying the source code even more it now auto-calls a global javascript function called "customRestore", which passes over information gathered from the intent and then goes through the normal routine of adding a photo.
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
I want to make a screenshot of the screen without saving the image, now I do this to make a screenshot:
View view = webView.getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
result = new PluginResult(PluginResult.Status.OK);
And to attach the image to the email:
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///INFINITA-PL.png"));
I do not know how to do it without path.
The EXTRA_STREAM value has to be a Uri that can be opened by the Email process. If you don't want to save it as a file and pass that along you will need to implement a ContentProvider to make it accessible. In general this is pretty messy to do: I can echo the comments in that question (I struggled for awhile to do it without hitting the file system before giving up).
You're probably better served using a File and moving on to the rest of your app.
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();