0

I'm trying to design an app and all I want it to do is load an image from a URL.I m displaying GIF image with the URL(from internet) but it is not displaying the image.I have posted code as well as logcat below:

Activity

package com.example.mosdacappalert_radio;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RelativeLayout;

public class Heavy_rain1 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heavy_rain1);
        RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.addView);

        myLayout.addView(new com.example.mosdacappalert_radio.ShowGifView(this,
                "http://graphico.in/wp-content/uploads/2014/05/Happy-anniversary-wishes-to-you.gif"));
    }
}


class file

package com.example.mosdacappalert_radio;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;

public class ShowGifView extends View {

    // Set true to use decodeStream
    // Set false to use decodeByteArray
    private static final boolean DECODE_STREAM = true;

    private InputStream gifInputStream;
    private Movie gifMovie;
    private int movieWidth, movieHeight;
    private long movieDuration;
    private long mMovieStart;

    static String gifURL;

    public ShowGifView(Context context, String a) {
        super(context);
        gifURL = a;
        init(context);
    }

    public ShowGifView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ShowGifView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        setFocusable(true);

        gifMovie = null;
        movieWidth = 0;
        movieHeight = 0;
        movieDuration = 0;
        Loder task = new Loder();
        task.execute(new String[] { gifURL });

    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(movieWidth, movieHeight);
    }

    public int getMovieWidth() {
        return movieWidth;
    }

    public int getMovieHeight() {
        return movieHeight;
    }

    public long getMovieDuration() {
        return movieDuration;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) { // first time
            mMovieStart = now;
        }

        if (gifMovie != null) {

            int dur = gifMovie.duration();
            if (dur == 0) {
                dur = 1000;
            }

            int relTime = (int) ((now - mMovieStart) % dur);

            gifMovie.setTime(relTime);

            gifMovie.draw(canvas, 0, 0);
            invalidate();

        }

    }

    private class Loder extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            URL gifURL;
            try {
                gifURL = new URL(urls[0]);
                HttpURLConnection connection = (HttpURLConnection) gifURL
                        .openConnection();

                gifInputStream = connection.getInputStream();

                // Insert dummy sleep
                // to simulate network delay
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                if (DECODE_STREAM) {
                    gifMovie = Movie.decodeStream(gifInputStream);
                } else {
                    byte[] array = streamToBytes(gifInputStream);
                    gifMovie = Movie.decodeByteArray(array, 0, array.length);
                }
                movieWidth = gifMovie.width();
                movieHeight = gifMovie.height();
                movieDuration = gifMovie.duration();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            invalidate();
            requestLayout();

        }
    }

}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/addView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
     >
    </RelativeLayout>

</LinearLayout>


Logcat:

12-08 19:18:37.408: W/System.err(4521): java.io.IOException
12-08 19:18:37.428: W/System.err(4521):     at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:279)
12-08 19:18:37.428: W/System.err(4521):     at android.graphics.Movie.decodeStream(Native Method)
12-08 19:18:37.428: W/System.err(4521):     at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:136)
12-08 19:18:37.428: W/System.err(4521):     at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:1)
12-08 19:18:37.428: W/System.err(4521):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-08 19:18:37.428: W/System.err(4521):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-08 19:18:37.428: W/System.err(4521):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-08 19:18:37.438: W/System.err(4521):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-08 19:18:37.438: W/System.err(4521):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-08 19:18:37.438: W/System.err(4521):     at java.lang.Thread.run(Thread.java:841)
12-08 19:18:37.438: D/skia(4521): ------- reset threw an exception
12-08 19:18:37.438: W/System.err(4521): java.io.IOException
12-08 19:18:37.448: W/System.err(4521):     at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:279)
12-08 19:18:37.448: W/System.err(4521):     at android.graphics.Movie.decodeStream(Native Method)
12-08 19:18:37.448: W/System.err(4521):     at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:136)
12-08 19:18:37.448: W/System.err(4521):     at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:1)
12-08 19:18:37.448: W/System.err(4521):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-08 19:18:37.448: W/System.err(4521):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-08 19:18:37.448: W/System.err(4521):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-08 19:18:37.448: W/System.err(4521):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-08 19:18:37.448: W/System.err(4521):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-08 19:18:37.448: W/System.err(4521):     at java.lang.Thread.run(Thread.java:841)
12-08 19:18:37.448: D/skia(4521): ------- reset threw an exception
12-08 19:18:37.498: W/System.err(4521): java.io.IOException
12-08 19:18:37.518: W/System.err(4521):     at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:279)
12-08 19:18:37.518: W/System.err(4521):     at android.graphics.Movie.decodeStream(Native Method)
12-08 19:18:37.518: W/System.err(4521):     at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:136)
12-08 19:18:37.518: W/System.err(4521):     at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:1)
12-08 19:18:37.518: W/System.err(4521):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-08 19:18:37.518: W/System.err(4521):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-08 19:18:37.518: W/System.err(4521):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-08 19:18:37.518: W/System.err(4521):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-08 19:18:37.518: W/System.err(4521):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-08 19:18:37.518: W/System.err(4521):     at java.lang.Thread.run(Thread.java:841)
12-08 19:18:37.518: D/skia(4521): ------- reset threw an exception
12-08 19:18:37.518: W/System.err(4521): java.io.IOException
12-08 19:18:37.518: W/System.err(4521):     at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:279)
12-08 19:18:37.518: W/System.err(4521):     at android.graphics.Movie.decodeStream(Native Method)
12-08 19:18:37.528: W/System.err(4521):     at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:136)
12-08 19:18:37.528: W/System.err(4521):     at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:1)
12-08 19:18:37.528: W/System.err(4521):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-08 19:18:37.528: W/System.err(4521):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-08 19:18:37.528: W/System.err(4521):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-08 19:18:37.528: W/System.err(4521):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-08 19:18:37.528: W/System.err(4521):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-08 19:18:37.528: W/System.err(4521):     at java.lang.Thread.run(Thread.java:841)
12-08 19:18:37.528: D/skia(4521): ------- reset threw an exception
12-08 19:18:37.528: W/System.err(4521): java.io.IOException
12-08 19:18:37.528: W/System.err(4521):     at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:279)
12-08 19:18:37.528: W/System.err(4521):     at android.graphics.Movie.decodeStream(Native Method)
12-08 19:18:37.528: W/System.err(4521):     at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:136)
12-08 19:18:37.528: W/System.err(4521):     at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:1)
12-08 19:18:37.528: W/System.err(4521):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-08 19:18:37.528: W/System.err(4521):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-08 19:18:37.528: W/System.err(4521):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-08 19:18:37.528: W/System.err(4521):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-08 19:18:37.528: W/System.err(4521):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-08 19:18:37.528: W/System.err(4521):     at java.lang.Thread.run(Thread.java:841)
12-08 19:18:37.538: D/skia(4521): ------- reset threw an exception
12-08 19:18:37.538: D/skia(4521): QURAMWINKI_ParseGIFHeader_SKIA error
12-08 19:18:37.538: D/skia(4521): Wink AGIF Move Constructer End 0, totalTime : 0
12-08 19:18:37.558: D/skia(4521): Wink AGIF onGetBitmap Null
bhagyasri patel
  • 61
  • 1
  • 11

2 Answers2

0

From this SE answer it looks like animated GIF support using the Movie class is somewhat problematic (it doesn't work on older APIs and it fails on certain types of GIF encodings). Might be simpler to just use WebView.

Community
  • 1
  • 1
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
0

You can use

  1. Movie (http://developer.android.com/reference/android/graphics/Movie.html)
  2. WebView (http://developer.android.com/reference/android/webkit/WebView.html)

Movie class :- Introduced in API level 1 of android but GIF decoding is not working for all devices. Facing in-consistence problems on some versions of some devices like on Xperia L with 2.3.3 is not working etc.

So to avoid above issue simply go with WebView, just load GIF url in it.

Ganesh AB
  • 4,652
  • 2
  • 18
  • 29