我正在尝试从给定的网址下载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");
        }

最佳答案

您不能在主线程上运行网络操作。为此使用AsyncTask

08-18 13:52