尝试从URL加载可绘制图像时出现异常

时间:2013-03-01 11:24:07

标签: java android eof

以下是代码:

private static Drawable loadImageFromUrl(String url) {
    URL m;
    BufferedInputStream bis = null;
    Drawable d = null;
    try {
        m = new URL(url);
        URLConnection conn = m.openConnection();
        conn.connect();
        bis = new BufferedInputStream(conn.getInputStream());  <<--- EOF exception occurs.
        d =
                Drawable.createFromStream(bis,
                        url.substring(url.lastIndexOf("/") + 1, url.length()));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return d;
}

我的代码有什么问题吗?例外情况如下:

03-01 19:16:51.573: W/System.err(12554): java.io.EOFException
03-01 19:16:51.843: W/System.err(12554): java.io.EOFException
03-01 19:16:52.204: W/System.err(12554):    at libcore.io.Streams.readAsciiLine(Streams.java:203)
03-01 19:16:52.254: W/System.err(12554):    at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:547)
03-01 19:16:52.274: W/System.err(12554):    at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:787)
03-01 19:16:52.344: W/System.err(12554):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
03-01 19:16:52.354: W/System.err(12554):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
03-01 19:16:52.703: W/System.err(12554):    at com.gurudigger.model.task.UserPhotoTask.loadImageFromUrl(UserPhotoTask.java:36)
03-01 19:16:52.864: W/System.err(12554):    at com.gurudigger.model.task.UserPhotoTask.doInBackground(UserPhotoTask.java:78)
03-01 19:16:52.883: W/System.err(12554):    at com.gurudigger.model.task.UserPhotoTask.doInBackground(UserPhotoTask.java:1)
03-01 19:16:53.003: I/InputQueue-JNI(12554): Sending finished signal for input channel '4163b660 com.gurudigger/com.gurudigger.activity.IdeaListActivity (client)' since it is being unregistered while an input message is still in progress.
03-01 19:16:53.063: W/System.err(12554):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-01 19:16:53.124: W/System.err(12554):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-01 19:16:53.244: I/InputQueue-JNI(12554): Ignoring finish signal on channel that is no longer registered.
03-01 19:16:53.503: W/System.err(12554):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-01 19:16:53.513: W/System.err(12554):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-01 19:16:53.694: W/System.err(12554):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-01 19:16:53.773: W/System.err(12554):    at java.lang.Thread.run(Thread.java:856)
03-01 19:16:53.813: W/System.err(12554):    at libcore.io.Streams.readAsciiLine(Streams.java:203)
03-01 19:16:53.994: W/System.err(12554):    at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:547)
03-01 19:16:53.994: W/System.err(12554):    at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:787)
03-01 19:16:54.024: W/System.err(12554):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
03-01 19:16:54.024: W/System.err(12554):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
03-01 19:16:54.024: W/System.err(12554):    at com.gurudigger.model.task.UserPhotoTask.loadImageFromUrl(UserPhotoTask.java:36)
03-01 19:16:54.024: W/System.err(12554):    at com.gurudigger.model.task.UserPhotoTask.doInBackground(UserPhotoTask.java:78)
03-01 19:16:54.064: W/System.err(12554):    at com.gurudigger.model.task.UserPhotoTask.doInBackground(UserPhotoTask.java:1)
03-01 19:16:54.064: W/System.err(12554):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-01 19:16:54.144: W/System.err(12554):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-01 19:16:54.163: W/System.err(12554):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-01 19:16:54.224: W/System.err(12554):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-01 19:16:54.333: W/System.err(12554):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-01 19:16:54.354: W/System.err(12554):    at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:0)

为什么要创建已经存在的东西?只需使用这个库,它很容易使用。

URLImageViewHelper