Handing Drawing / Painting / Saving of multiple bitmaps - android

Goal:
I want to draw / write / paint on bitmaps from pdf and save them together so I could send them email.
Details:
I have multiple Pdf files containing 5-20 pages each, right now am extracting bitmaps from pdfs and loading them in fragments in ViewPager,
where I can swipe through them and write / draw or whatever I want. To save / restore the current state of the bitmap I'm using onSaveInstanceState.
This way I'm able to retrieve the last stage of the bitmap while swiping back and forth.
Something like this:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("Bitmap", bitmap);
}
Problem:
User presses send email button in and I should be able to save all the images regardless of which page he is on i.e. he has edited the page 5 to 10
and now he is on page 10 and press the send button I should be able to retrieve all the edited images.
Problem 2:
As I have stated the number of images are about 5-20, Saving the state in onSaveInstanceState works great but it keeps increasing the app memory in ram, and the app crashes at 10th image where the app size goes to 150Mbs.
But saving the image as bundle in onSaveInstanceState is also necessary because it helps for faster loading of images when user swipes back n forth.
So I was thinking is there any way we can create a custom bundle
class where we can write the image to disk after say 5 pages scrolled
and clears the memory and read it back from disk to memory when user
is scrolling back? does this sound any reasonable?
What I have tried:
As bitmaps are loaded in seperate fragments I tried to save the bitmaps to external storage in OnDestroy, but as saving bitmaps to disk is not a small task, it takes time though am using AsyncTask
but when user is swiping bit fast between fragments then saving bitmaps in OnDestroy does not seems like a convinient way.
This is why I'm asking the question so maybe you have better understanding of the situation and can help me achieve this in a more convinient way.
So what do you purpose how I can handle this situation?
Note:
if you have trouble understanding the situation and need more info leave me a comment and I will update the question.
Thanks

Let me describe how i am seeing the solution of this issue now.
There should be a Service which takes an edited bitmap with some id (same for all modifications of original bitmap), adds it to queue and then each item of that queue it persists to the external storage. It has limited Executor instance which executes that tasks (them could be just Runnable) and Service creates a HashMap where maps Future of task to bitmap identifier. So when new Bitmap has came, Service checks the map and replaces waiting task (with same original bitmap) if any have existed.
You can send a task to this Service from destroyItem interceptor of FragmentStatePagerAdapter.
So, when user clicks on save button you should also send all edited bitmaps left to this Service and just wait for completion.

Related

When to Load image in ListView?

When I use ListView the getView() method is called many times. Every time when the getView() is called i load the image with Asyc task. I mean every time i reset the image which is annoying.
How to understand when to load the image?
You should cache loaded images, by storing i.e. on SD card, so once you got a copy there, no need to download it again. There's lot of ready-to-use classes that can do the job for you, like:
http://greendroid.cyrilmottier.com/reference/greendroid/widget/AsyncImageView.html
you must must have two flags.
One which says if you've already loaded the image, if true you do nothing.
One which says if you're currently loading the image, if true you do nothing.
The members will also help you on maintaining the state of the image.
Your code should look something like this:
private boolean isLoading = false;
private boolean hasLoaded = false;
if(!hasLoaded){
if(!isLoading){
isLoading = true;
//do async load
//on positive completition callback set hasLoaded to true
//on negative completition callback set isLoading to false
}
}
One of the best solution is to create image cache using the WeakReference. This way you can keep images in memory and only need load from server when they are not in memory. In this method the image would be removed from the memory when system encounter low memory situation. So your current activity would always keep the hard reference to the bitmap's required and the image cache would keep the weak reference to the bitmap's.
below reference links will help you
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
http://www.codeproject.com/Articles/35152/WeakReferences-as-a-Good-Caching-Mechanism
the Volley library (made by google) has a very intuitive class for an imageView that can have a url , called "NetworkImageView" .
you should check it out and watch the video, since they show that it's quite annoying to do it using asyncTask (plus the asyncTask is known to have a limit of tasks, about 255 or so) .
for setting the url, just use setImageUrl .
it has some useful methods for the phases of loading too: setDefaultImageResId , setErrorImageResId.
it's also supposed to have built in caching mechanism of some sort, but i haven't read much about it, so you might want to check out their samples.
this will remove the need to use asyncTasks for the listView's items.
one of my questions regarding the volley includes a sample code , here .
You can add a caching layer and optionally preloading the images. A good strategy for caching Images (Bitmap objects to be exact) is to use a strategy called LRU or least recently used.
Android support library has a class called LruCache that implements this strategy. So for example, when you download/load the image for the first time, you stick it into the cache. later, you can first check if it's already in cache and load it from there.
For preloading, A good rule of thumb is to preload the previous ten and the next ten items.

What should be used for executing a task repeatedly at fixed intervals in android?

In my android application the scenario is this -
I have several images stored in the SD card.
Now in my app I have an Image view and I have to update this image view continuously
like a slide show is running. This has to be done by fetching images one by one from SD card and setting them in the same ImageView. Also the interval of image change in ImageView is fixed.
So I have a cursor which stores the information for fetching these images.
Now I am wondering what should be my approach for moving the cursor one by one and fetching images from SD card and setting them in my ImageView.
Should I use Timer Task and Timer or should I go with Handler or something else?
Please help as this is initial point of my app and I want to start in the right direction.
Since you're retrieving large data from the SD card, it might be best to use a thread to retrieve/decode the bitmap.
In this case, a Timer would be useful because it runs on it's own Java thread for scheduled periods of time. However, you have to keep in mind that you can't update a UI element from a non-UI thread. Thus, you'd need a syncing element in place for when the bitmap is fully loaded to finally put the decoded bitmap in to the ImageView. In which case, you would need to use Handler anyway to send message queues to the UI thread.
Thus, I'd probably recommend using a combination of Handler and AsyncTask. Create an AsyncTask class that retrieves and decodes the bitmap in doInBackground(). Update the ImageView in onPostExecute(). Use a Handler to create and execute a new AsyncTask ever Nth milliesecond (whenever you update the view).
One advantage to this is it keeps all image retrieval in one class. You can open the cursor, retrieve, close it. This is safer than keeping a long-running cursor open.

Best practice for storing images to use on short term

So, I'm using custom number tiles for images. The images are stored as resources. I've been tracing a memory leak, and have cause to believe the method I am storing these images for use is suspect. Currently, I'm doing this:
private void loadImageList()
{
Log.d(TAG,"Reloading List "+imageList);
if (imageList==null || imageList.size()<10)
{
imageList=new ArrayList<Bitmap>(10);
imageList.add(0,BitmapFactory.decodeResource(getResources(), R.drawable.ant_number0));
imageList.add(1,BitmapFactory.decodeResource(getResources(), R.drawable.ant_number1));
imageList.add(2,BitmapFactory.decodeResource(getResources(), R.drawable.ant_number2));
imageList.add(3,BitmapFactory.decodeResource(getResources(), R.drawable.ant_number3));
imageList.add(4,BitmapFactory.decodeResource(getResources(), R.drawable.ant_number4));
imageList.add(5,BitmapFactory.decodeResource(getResources(), R.drawable.ant_number5));
imageList.add(6,BitmapFactory.decodeResource(getResources(), R.drawable.ant_number6));
imageList.add(7,BitmapFactory.decodeResource(getResources(), R.drawable.ant_number7));
imageList.add(8,BitmapFactory.decodeResource(getResources(), R.drawable.ant_number8));
imageList.add(9,BitmapFactory.decodeResource(getResources(), R.drawable.ant_number9));
}
}
public void onStart()
{
super.onStart();
loadImageList();
}
What will happen is if I open and close this application repeatedly, then the system won't have enough memory to add an image to the ImageList. I'm setting the image of the button like this (But with an object ImageButton, of course). I should add that the application is threaded, and this call resides in a runOnUiThread(Runnable)
ImageButton.setImageBitmap(imageList.get(current_var));
I've tried deleting the images in the onStop() command, but it will sometimes cause a crash when the thread tries to allocate the image stored in memory to the button, due to the threaded nature of the beast.
So, is there a better way that I can load these images that won't cause a memory leak?
I realized something after posting this. I could just make the imageList static. These images aren't going to change from one view to the next. The Bitmaps don't contain a view, so the memory won't lock up. I'm already checking to see if the values exist before I'm using them. And this function is used so frequently that holding on to static memory won't hurt other aspects of the program. Sometimes it just helps to put it into words..

ListView asynchronous image loading strategy

I currently have a ListView with a custom adapter that gets information describing the content of the rows asynchronously. Part of each row is an image URL, that I'm planning to download asynchronously and then display.
My current plan for a strategy to download these images is:
Keep a cache of soft references to downloaded Bitmap objects.
When a getView() is called and the bitmap is in the cache, set the bitmap for the ImageView directly.
If the bitmap isn't in the cache, start loading it in a separate thread, after the download is complete add it to the cache and call notifyDataSetChanged() on the adapter.
I am also planning to kill pending downloads when the Activity object owning the ListView's onDestroy()-method (Or possibly even in the onPause()-method) is called, but most importantly I want to kill the download of pending images when the row goes off screen. I might only actually cancel the download after a short delay, so it can be resumed without wasting bandwidth if the row comes on-screen quickly again.
I, however, am unsure about a few things:
What is the best way to detect when a row goes off-screen so I can cancel the download?
Is calling notifyDataSetChanged() the best thing to do after the download has completed or is there a better way?
Also any comments on the whole strategy would be appreciated.
I don't think calling notifyDataSetChanged() is really needed... I would do it like that:
store URL as Tag in the view when created/updated
register a listener in downloader thread (async task???) for download keeping reference to the view and the URL
whenever image is downloaded asynchronously, I check TAG in the view and if it matches - i would update the ImageView (important to do it in UI thread, but when using async task, it is given). The image should also be stored on SD card (and every time you request URL you should check if it is not already downloaded).
every time when getView() reuses the view (passed view is not empty) I would check the Tag (old URL), replace it with the new URL and cancel the download of the oldURL.
I think it would be pretty much it (some corner cases might happen)...
I use the getFirstVisible and getLastVisible AdapterView properties to detect the visible rows, and put requests in a fixed size stack.
My project is open source and has a most permissive license, if you want to use it:
https://github.com/tbiehn/Android-Adapter-Image-Loader
-Travis
I found the remote resource managing / fetching in the Foursquared source code to be pretty helpful:
http://code.google.com/p/foursquared/source/browse/main/src/com/joelapenna/foursquared/util/RemoteResourceManager.java
It caches images on disk and handles all 3 of your feature requests. See an adapter for how to use it.
As for canceling a download when a row goes off screen you'll have to handle that yourself

android :activity

i have two activities
1) downloading something on oncreate function with help of asyn task ,and had one button.
2) secons activity display on click of button.
now when i go back to previous ie first activity , then downloading starts again, i want to get the previous filled data view ,instead of startg the previous process again ..
canu please guide me
thanks
You should set a default value to whatever it is you're downloading, for example a default image, text or number, and then before downloading again, check whether the stored value is the default one or something new. If it's new, then you don't have to download again.
For example, in my game I have a similar thing. It's a jigsaw puzzle game with many images, where all the images are displayed in a ListView. To save space, I didn't include both the full size image and a thumbnail, but instead generate the thumbnails when the game loads and saved in a Bitmap[] array. So that bitmap generation process is similar to your downloading.
Whenever my game is about to load a list or access the images, it first checks whether the array is null. If it is, then it restarts the loading process. If it isn't, then it can use them. This is done with a simple check in onResume():
if (imageThumbnails == null) {
// Do something to reload the images
} else {
//the images are available, so they can be used
}
You should be able to do something similar for your app.

Categories

Resources