0

I'm playing gif animation in my app. Its working good, but if I'm playing so many times same animation, its throw nullpointer exception in movie object.

any suggestion about this problem. below is my code

SampleView.class:

 class SampleView extends View {
           java.io.InputStream is;


            private Movie mMovie;
            private long mMovieStart;

            private  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();
            }

            public SampleView(Context context) {
                super(context);



                    if(is==null)
                    {

                try {
                    is =context.getAssets().open("a.gif");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }


                    mMovie = Movie.decodeStream(is);

            }

            public SampleView(Context context, AttributeSet attrs){
                super(context,attrs);

                    if(is==null)
                    {

                try {
                    is =context.getAssets().open("a.gif");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }

                mMovie = Movie.decodeStream(is);
            }
            public SampleView(Context context, AttributeSet attrs, int defStyle){
                super(context,attrs,defStyle);

                    if(is==null)
                    {

                try {
                    is =context.getAssets().open("a.gif");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }

                    mMovie = Movie.decodeStream(is);
            }


            @Override protected void onDraw(Canvas canvas) {

                long now = android.os.SystemClock.uptimeMillis();
                if (mMovieStart == 0) {   // first time
                    mMovieStart = now;
                }
                if (mMovie != null) {
                    int dur = mMovie.duration();
                    if (dur == 0) {
                        dur = 1000;
                    }
                    int relTime = (int)((now - mMovieStart) % dur);
                    mMovie.setTime(relTime);
                    mMovie.draw(canvas, getWidth() - mMovie.width(),
                                getHeight() - mMovie.height());
                    invalidate();
                }
            }
        } 
RobinHood
  • 10,897
  • 4
  • 48
  • 97
radha
  • 192
  • 1
  • 2
  • 13

1 Answers1

0

Try this,

public class GIFDemo extends GraphicsActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new GIFView(this));
}

private static class GIFView extends View {

    Movie movie;
    InputStream is = null;
    long moviestart;
    long moviestart1;

    public GIFView(Context context) {
        super(context);
        is = context.getResources()
                .openRawResource(R.drawable.animated_gif);

        movie = Movie.decodeStream(is);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFCCCCCC);
        super.onDraw(canvas);
        long now = android.os.SystemClock.uptimeMillis();
        System.out.println("now=" + now);
        if (moviestart == 0) { // first time
            moviestart = now;

        }

        System.out.println("\tmoviestart=" + moviestart);
        int relTime = (int) ((now - moviestart) % movie.duration());
        // int relTime1 = (int) ((now - moviestart1) % movie1.duration());
        System.out.println("time=" + relTime + "\treltime="
                + movie.duration());
        movie.setTime(relTime);
        // movie1.setTime(relTime1);
        movie.draw(canvas, 10, 10);
        // movie1.draw(canvas, 10, 100);
        this.invalidate();
    }
}
}

Add this GraphicsActivity calss.

  class GraphicsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void setContentView(View view) {

    super.setContentView(view);
}
}
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
  • sory,i tried using ur code.but now also i am getting exception.my log result is – radha Feb 27 '13 at 06:25
  • java.lang.NullPointerException 02-27 11:52:09.737: E/AndroidRuntime(28198): at android.graphics.Movie.decodeStream(Native Method) 02-27 11:52:09.737: E/AndroidRuntime(28198): at com.example.kidsletters1.A$SampleView.(A.java:1081) 02-27 11:52:09.737: E/AndroidRuntime(28198): at com.example.kidsletters1.A.onCreate(A.java:111) – radha Feb 27 '13 at 06:26
  • @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alh); sampleview=new SampleView(this); lp6 = new RelativeLayout.LayoutParams( android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); sampleview.setLayoutParams(lp6); lp6.setMargins(0,topsampl , 0, 0); – radha Feb 27 '13 at 06:49
  • i want xml file also so i gave like this,using custom class sample view – radha Feb 27 '13 at 06:50
  • in same GraphicsActivity class i want display some xml stuff also – radha Feb 27 '13 at 06:53
  • sory,i didnt get u.i want to play gif file,webview means we can display either url,html file.i dont know how to play gif file using webview – radha Feb 27 '13 at 07:10
  • ok create index.html in your assets . write this code
    – MuraliGanesan Feb 27 '13 at 07:20
  • and then create gif folder inside folder and then paste your gif image into that folder – MuraliGanesan Feb 27 '13 at 07:21
  • and then use webview. webTutorial = (WebView) findViewById(R.id.webTutorial); webTutorial.getSettings().setUseWideViewPort(false); webTutorial.setPadding(0, 0, 0, 0); webTutorial.setInitialScale(0); webTutorial.loadUrl("file:///android_asset/index.html"); – MuraliGanesan Feb 27 '13 at 07:23
  • to avoid webview scroll able use this code webTutorial.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return (event.getAction() == MotionEvent.ACTION_MOVE); } }); – MuraliGanesan Feb 27 '13 at 07:23
  • let me know any clarification. ask me – MuraliGanesan Feb 27 '13 at 07:24
  • but i want draw functionality also – radha Feb 27 '13 at 07:26
  • i want to display alphets in on draw and if i click any button i need to play gif animation like tracing that alhabets – radha Feb 27 '13 at 11:44