Download gif from given URL

2019-08-14 14:12发布

问题:

I'm trying to download GIF from given Url but there is a problem. I'm stuck here and don't know the correct way to download this file. How to repair this? I have sample url: http://i1.kwejk.pl/k/obrazki/2015/02/6f0239004a643dbd9510ebda5a6f22b3.gif

My code:

try{
            System.out.println(this.adressToGif);
            URL url = new URL(urltoGif);
            URLConnection conn = url.openConnection();
            //conn.connect();
            InputStream is = conn.getInputStream(); //IT crashes here and jumps to catch()
            BufferedInputStream bis = new BufferedInputStream(is);
            bis.mark(conn.getContentLength());
            bis.close();

            gifPlayer = new NewGifPlayer(this, bis);

            //Movie movie = Movie.decodeStream(bis);


        }catch(Exception x)
        {
            System.out.println("There is a problem");
        }

回答1:

You cannot run networking operations on the main thread. Use AsyncTask for that.