I am developing an android application , in which i am using a gif images with the following code
private static byte[] streamToBytes(InputStream is) {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int len;
try {
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
} catch (java.io.IOException e) {
}
return os.toByteArray();
}
// In constructor of the class
is = this.context.getResources().openRawResource(R.drawable.fondo_juego_nexus);
if (DECODE_STREAM) {
System.out.println("in if DECODE_STREAM");
mMovie = Movie.decodeStream(is);
}
else {
byte[] array = streamToBytes(is);
System.out.println("in else DECODE_STREAM");
mMovie = Movie.decodeByteArray(array, 0, array.length);
}
// In On Draw
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (mMovie != null) {
System.out.println("in if (mMovie != null) " + mMovie.duration());
int dur = mMovie.duration();
if (dur == 0)
{
dur = 1000;
System.out.println("in if movie if");
}
System.out.println("duration is "+ dur);
int relTime = (int)((now - mMovieStart) % dur);
mMovie.setTime(relTime);
System.out.println("in if displaying syd");
mMovie.draw(canvas,120,100);
}
and by ontouch i exit from activity like
else if(_y<=60 && _x<=60)
{
sp.play(mySound1, volume, volume, 0, 0, 1);
sp.release();
playr.stop();
tme.cancel();
act.finish();
}
Bu when i exit activity with above method and go back to previous activity ,
and come again to the activity where i am using gif image it does not appear , on device galaxy s2 , 2.3.3 but on an emulator of same size of 2.2 it is fine
is there any problem with this approach or what way i can use to display a gif image
what should i do to remove this error
I got the solution for my problem , i do not know how good it is ,
but it worked for me earlier i was using this code in constructor
It was workign fine first time, but i think in second time it was running out of memory due to
too much of garbage collector which was running implicitly
and my image initialization was also running again and again which was the cause of the
bad performance of the game ,and
what i did was that i just added
the garbage collector manually with
and initialized all images after this and also
placed the following code
after this
which avoided the implicit running of garbage collector
and now the gif image is working fine