Issue
I have some images in my app on sd card and i want to show these images to user one by one for some duration, lets say one image then other one after one minute then another one after one minute like this.I am using aysnctask for this in which i convert the image in a bitmap and show it.I want to show these images in a continous loop until user exists from the app the app should show images one by one i am using the following code for it:-
for(i=1;i<z-1;i++)
{
xc=cont_id.get(1).toString();
what=Environment.getExternalStorageDirectory() + "/Playerimages/" + xc + ".jpg";
play_duration=durat.get(1);
//s1.execute(what,play_duration);
new Showtime().execute(what,play_duration);
Log.i("lenght",String.valueOf(i));}
but when i try to execute it in an endless loop like this my app crashes:-
for(i=1;i<z-1;i++)
{
xc=cont_id.get(1).toString();
what=Environment.getExternalStorageDirectory() + "/Playerimages/" + xc + ".jpg";
play_duration=durat.get(1);
//s1.execute(what,play_duration);
new Showtime().execute(what,play_duration);
Log.i("lenght",String.valueOf(i));
if(i=Z-1){
i=1;
}}
my app crashed please someone suggest how can i achieve it.. now my app is not crashing but it is not showing any images my logcat is below
09-04 12:11:31.807: I/dalvikvm-heap(4358): Clamp target GC heap from 48.002MB to 48.000MB
09-04 12:11:31.807: I/dalvikvm-heap(4358): Grow heap (frag case) to 48.000MB for 80-byte allocation
09-04 12:11:32.182: I/dalvikvm-heap(4358): Clamp target GC heap from 50.002MB to 48.000MB
09-04 12:11:32.182: D/dalvikvm(4358): GC_FOR_ALLOC freed 0K, 2% free 48571K/49159K, paused 372ms, total 372ms
09-04 12:11:32.182: I/dalvikvm-heap(4358): Clamp target GC heap from 48.002MB to 48.000MB
09-04 12:11:32.182: I/dalvikvm-heap(4358): Grow heap (frag case) to 48.000MB for 20-byte allocation
09-04 12:11:32.182: D/dalvikvm(4358): WAIT_FOR_CONCURRENT_GC blocked 8390ms
09-04 12:11:32.557: I/dalvikvm-heap(4358): Clamp target GC heap from 50.002MB to 48.000MB
09-04 12:11:32.557: D/dalvikvm(4358): GC_FOR_ALLOC freed 0K, 2% free 48571K/49159K, paused 380ms, total 380ms
09-04 12:11:32.557: I/dalvikvm-heap(4358): Clamp target GC heap from 48.002MB to 48.000MB
09-04 12:11:32.557: I/dalvikvm-heap(4358): Grow heap (frag case) to 48.000MB for 12-byte allocation
09-04 12:11:32.932: I/dalvikvm-heap(4358): Clamp target GC heap from 50.002MB to 48.000MB
09-04 12:11:32.932: D/dalvikvm(4358): GC_CONCURRENT freed 0K, 2% free 48571K/49159K, paused 2ms+3ms, total 371ms
09-04 12:11:32.932: D/dalvikvm(4358): WAIT_FOR_CONCURRENT_GC blocked 369ms
09-04 12:11:33.292: I/dalvikvm-heap(4358): Clamp target GC heap from 50.002MB to 48.000MB
09-04 12:11:33.292: D/dalvikvm(4358): GC_FOR_ALLOC freed 0K, 2% free 48571K/49159K, paused 362ms, total 362ms
09-04 12:11:33.292: I/dalvikvm-heap(4358): Clamp target GC heap from 48.002MB to 48.000MB
09-04 12:11:33.292: I/dalvikvm-heap(4358): Grow heap (frag case) to 48.000MB for 110-byte allocation
09-04 12:11:33.659: I/dalvikvm-heap(4358): Clamp target GC heap from 50.002MB to 48.000MB
09-04 12:11:33.659: D/dalvikvm(4358): GC_FOR_ALLOC freed <1K, 2% free 48571K/49159K, paused 362ms, total 362ms
09-04 12:11:33.659: I/dalvikvm-heap(4358): Clamp target GC heap from 48.002MB to 48.000MB
09-04 12:11:33.659: I/dalvikvm-heap(4358): Grow heap (frag case) to 48.000MB for 24-byte allocation
09-04 12:11:34.026: I/dalvikvm-heap(4358): Clamp target GC heap from 50.002MB to 48.000MB
09-04 12:11:34.026: D/dalvikvm(4358): GC_FOR_ALLOC freed <1K, 2% free 48571K/49159K, paused 368ms, total 368ms
09-04 12:11:34.026: I/dalvikvm-heap(4358): Clamp target GC heap from 48.002MB to 48.000MB
and much more like this
Solution
When you use a for loop it's going to try to do whatever it is you're doing as rapidly as it can. new Showtime().execute() is not a blocking call, so, you're basically spawning off thousands and thousands of threads loading images and very quickly running out of memory.
So... assuming that Showtime is your AsyncTask you want to fire off the next iteration of your image display from when that task completes.
Remove the for() loop and just use your Task as the scheduler which shows the image and then waits and then shows the next one.
Also, without seeing your stack trace it's impossible to know what else you might be doing wrong, but I'm taking for granted that you're loading your Bitmaps correctly (which you very well may not be).
[EDIT] You don't actually need an Async task. You can just do this in a runnable that does postDelayed like this:
public void onCreate(Bundle b){
super.onCreate(b);
//load your Layout
//start your image rotator
new Handler().post(showNextImage);
}
Runnable showNextImage = new Runnable(){
public void run(){
//draw your next image here
//now schedule your next image
new Handler().postDelayed(showNextImage, 60000);// show next image in a minute
}
}
Answered By - Yevgeny Simkin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.