从给定的URL下载gif

时间:2015-02-11 12:44:54

标签: android urlconnection

我试图从给定的Url下载GIF,但是存在问题。我被困在这里并且不知道下载此文件的正确方法。怎么修这个?我有示例网址:http://i1.kwejk.pl/k/obrazki/2015/02/6f0239004a643dbd9510ebda5a6f22b3.gif

我的代码:

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 个答案:

答案 0 :(得分:2)

您无法在主线程上运行网络操作。请使用AsyncTask