Method to pass an image value? - android

How would i go about passing an image to the next page for android?

You have a few different choices here.
I assume by "next page" you mean the next Activity? I also assume you've created a Bitmap out of the image. Bitmap implements Parcelable so you could just put it directly into the extras of the intent. This also lets you can put it in the Bundle in your activity's onSaveInstanceState() method, so it is restored whenever that activity is rotated or is recreated for some other reason.
However, if you have more than one image, this is less than ideal, since you have to serialize and deserialize all your images each time you switch activities. If you obtained the image from the file system somewhere, or if you got it from a ContentProvider like the MediaStore, then you'll have a Uri for it. So, you could just put the Uri in the intent, and then recreate the bitmap each time you load the activity. This has the advantage of being a smaller amount of serialized data, but it's even worse in terms of processing because now you have to read from the filesystem and decompress the image every time.
Therefore, if you are concerned with performance, the only good method is to store the image(s) to a static variable so that it can be accessed by your other activities. This way, both activities actually use the same image instead of duplicating it, thereby saving memory. The only disadvantage with this approach is that you will not be able to start the activity in a new task. All activities that use the image must run in the same process. Also, if you are manually recycling the image when you're done (via the Bitmap.recycle() method), then you'll have to make sure no one else is using the image before you recycle it.
Personally, a lot of my apps download images from a server and I store all HTTP responses in a cache, so whenever I need one of these images I re-request it from the cache. The cache is a singleton so it can be accessed from any of my activities.

Do you have a sample code for passing an image value?
Will this work?
Intent i = new Intent(Moods.this, New_Entry.class);
Bundle f = new Bundle();
f.putString("image", img);

Related

what is the best way to pass image from one activity another in android

In my apps, i need to pass image of image view from one activity to another. I know there are different kinds of way to pass image from activity to activity. But i want to know the best approach to do that. Previously i tried to pass image by getting the Bitmap of imageview from first activity then put in as putextra to the intent then extract the bitmap by getPercible to second activity. This was working good in Lollipop but getting error in Nougat. Now, i am trying to pass the bitmap as byteArray, but this is more unreliable than previous one. I don't know how to overcome this situation. Please help me in this context.
Where do you get your image from?
I would advise you to store it locally and only pass the path to it to the other activity. In this activity just retrieve the Bitmap again from your local storage.

Register next Activity (Activity B) in previous Activity (Activity A) as Callback for AsyncTask

I have a very specific use case I want to implement but I am not quite sure how to do it.
Scenario:
I have an activity (call it ActivityA) which loads (at most 10) images from specific URLs and shows them in a custom ViewPager. I created a class (lets say AttributedPhotos) that holdes the bitmaps and the the attribution of it. This class implements Parcelable.
Then I registered an OnTouchListener to this ViewPager so the images are opened in another Activity in fullscreen if the user touches the ViewPager and the user can navigate through them by swiping left and right (and pinch to zoom and so forth, basically interact with them in fullscreen). At first I tried to pass the images as an ArrayList of AttributedPhotos (intent.putParcelableArrayListExtra("attr-photos", myPhotos)) to the next Activity but then got the error message E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! which apparently means that there is not enough space in the Intent for this ArrayList. Which makes sense because the ArrayList contains 10 500x500 bitmaps. So I searched in StackOverflow and found a solution to my problem here which is basically storing the images in the device memory in ActivityA and loading them in ActivityB. I implemented helper classes that read/write the images from/to the cache directory asynchronously (ImageLoader and ImageSaver, both with static methods). This seems to work for me BUT I want ActivityB to only load the images if ActivityA has finished saving them and that's where my problem is.
Question
Is there a way to notify ActivityB that ActivityA finished writing the images to the cache? I thought of something like:
Activity A:
ImageSaver saver = new ImageSaver(...);
saver.setCallback(activityB);
saver.execute();
startActivity(activityB);
Activity B implements OnSavedListener:
#Override
public void onImagesSaved() {
ImageLoader loader = new ImageLoader(viewPager,...); // ViewPager instance to set the images after loading (in onPostExecute())
loader.execute();
}
Brief explanation: I want activity A to set activity B as a callback object so that ImageSaver calls activityB if all images are saved. Meanwhile activityA started activityB (which shows a progress spinner until the images are loaded) ... or something similar to this.
For now I send activityB the size of the arraylist via intent and implemented a method to check if the number of files in the appropriate cache-subdirectory equals the size of the arraylist of AttributedPhotos in activityB and call this method every second. Only if the number of files equals the size of arraylist, the images are loaded. This is obviously a dirty hack. At least for me it seems like a dirty hack. One problem with this solution is that if the last bitmap needs for example 5 seconds to be written then activityB cant read the last image because the number of files is correct as soon as the last file is created but as reading is much faster than writing, activityB could read all files before the last one had been written completely.
I would like to have a more robust solution if possible. Preferably something like the pseudo code above.
Thank you in advance!

How to save data members of Fragment during configuration change?

I have a fragment that creates an AsyncTask to download and parse a RSS feed, then displays it in a list. The problem is, downloaded feed is stored in a RSSFeedobject, and that becomes null when the fragment is recreated after a screen rotation. That means every time the user rotates the screen, the app must re-download the feed, wasting time and bandwidth. I could have it load a cached copy of the xml but that still takes time and is bad UX.
Up until now I've been using setRetainInstance(true) in the fragment, and it seems to work. However, I've recently read that it is not recommended to use setRetainInstance(true) in a fragment with a UI, something about leaking context. I've also seen other people say that it's ok and even recommended, as long as you reassign a value to views once the activity is recreated. I'm not sure how accurate either answer is, some help here would be appreciated.
Assuming I don't use setRetainInstance(true), and the fragment is recreated on configuration change, I'd like a way to preserve just that object. If it was a string or int I know to use onSaveInstanceState and a bundle, but the thing is this feed isn't serializable, nor is it recommended to serialize and deserialize a potentially large object. So the second question is, what can I do to preserve the feed so I don't have to reload it again?

Starting an activity with large intent extras

I am starting an activity with a Serializeable extra. This extra contains a List of a custom object holding a bunch of types, mostly Strings. I read in the data from the assets folder of my project, and parse it with GSON(the data is JSON). This file is ~108KB in size.
For the life of my application, all data is being passed around as intent extras. This is very convenient as i don't have to worry about re-loading the data from the assets folder again, app shutdown recovery is all taken care of, and i don't need to manage an SQLite database(versioning, queries, etc).
Problem:
I find that passing these extras around can become quite slow(starting an activity with ALL the data takes maybe 1.5 seconds or more). I can't seem to show any "loading" dialog either, as it seems to be a blocking call to start an activity and attach extras.
Question(s):
Should I avoid passing these extras around like I have described? Is the best option using an SQLite database to interact with this data? What suggestions do you have to avoid a lot of the fuss of SQLite databases/global static variables for accessing my application's data?
Slapping my JSON data into data model classes and passing them as intent extras is easy and nice(seemingly), and i don't want to give it up!
I had the same problem and followed the solution you already depicted. I used an SQLite DB to store the information, then passed only unique ids as intent extra and displayed a "loading..." progress dialog on the called activity while I accessed the DB.
Prolly using SQLite is the way to go.
Why don't you load the data to the Application class, onCreate of the Application class, and then access from every activity there?

Android: sharing an image cache among activities

I am developing an Android application and can't quite figure out the best way of implementing a 2-level image cache that can be shared among multiple activities w/in a single application.
Example:
Application has 3 activities (A, B, and C) and for the sake of argument lets say that A calls B and B calls C and C calls A. Each activity displays an image downloaded from the web and I'm using asynctask to download and display images w/in each activity - easy enough. Now I'd like to add an image cache to avoid multiple downloads of the same image.
Right now each activity starts a new instance of a simple asynctask that downloads the image and updates the view appropriately. Obviously its easy enough to update the basic asynctask to check the image cache before proceeding to download and to update the cache once the download is complete but I'm stuck on how/where to create and initialize the cache. Any thoughts would be appreciated.
You can add this to your app's manifest:
application android:name
="MyApplication" (...)
You can then create a class that has the name "MyApplication". You can then use that class across your activities. Check, before making the async call, if you already have a proper image to use. If you have, you use the one "cached", if not, you can get a new one. You can try something like this (in this case to get some random strings):
ArrayList myStrings =
((MyApplication)
this.getApplication()).getRandomStrings();
Hope this helped you. :)
Edit: Don't forget to the create your "MyApplication" like this:
public class MyApplication extends
Application

Categories

Resources