在后台android下载图像

时间:2013-02-28 04:10:12

标签: java android multithreading asynchronous background-image

我正在尝试在后台下载图片,以便在下载图片时应用不会挂起。我得到一个nullpointerexception,我不明白我做错了什么。我的日志说我正在按照我的意愿从URL中获取数据,所以我不确定为什么这不起作用。谢谢!

以下是我的代码:

public class DownloadImageBackground extends AsyncTask<String, Void, Drawable> {

ImageView imageView = null;
String myURL = null;

@Override
protected Drawable doInBackground(String...strings) {
    this.myURL = strings[0];
    Log.i("doInBackground", "Loading image from: "+myURL.toString());
    //TODO: Pass the correct URL to download_image
    return download_Image(myURL);
}

@Override
protected void onPostExecute(Drawable result) {
    imageView.setImageDrawable(result);
}

private Drawable download_Image(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, url);
        return d;
    } catch (Exception e) {
        Log.e("download_Image", "Caught exception: "+e.getMessage());
        return null;
    }
}
}

这是错误:

02-27 20:07:37.815: I/doInBackground(19374): Loading image from: http://www.martingrumet.com/boxelder01may04.jpg
02-27 20:07:37.815: I/Choreographer(19374): Skipped 92 frames!  The application may be doing too much work on its main thread.
02-27 20:07:37.840: D/dalvikvm(19374): GC_CONCURRENT freed 404K, 9% free 13752K/15047K, paused 2ms+12ms, total 24ms
02-27 20:07:38.110: D/dalvikvm(19374): GC_FOR_ALLOC freed 72K, 9% free 13723K/15047K, paused 11ms, total 11ms
02-27 20:07:38.110: I/dalvikvm-heap(19374): Grow heap (frag case) to 15.182MB for 1334016-byte allocation
02-27 20:07:38.125: D/dalvikvm(19374): GC_CONCURRENT freed 0K, 9% free 15026K/16391K, paused 2ms+1ms, total 12ms
02-27 20:07:38.125: D/dalvikvm(19374): WAIT_FOR_CONCURRENT_GC blocked 11ms
02-27 20:07:38.395: D/AndroidRuntime(19374): Shutting down VM
02-27 20:07:38.395: W/dalvikvm(19374): threadid=1: thread exiting with uncaught exception (group=0x40cf14a0)
02-27 20:07:38.395: E/AndroidRuntime(19374): FATAL EXCEPTION: main
02-27 20:07:38.395: E/AndroidRuntime(19374): java.lang.NullPointerException
02-27 20:07:38.395: E/AndroidRuntime(19374):    at com.ourbenefactors.treeidentification2.DownloadImageBackground.onPostExecute(DownloadImageBackground.java:27)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at com.ourbenefactors.treeidentification2.DownloadImageBackground.onPostExecute(DownloadImageBackground.java:1)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.os.AsyncTask.finish(AsyncTask.java:631)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.os.Looper.loop(Looper.java:137)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at java.lang.reflect.Method.invoke(Method.java:511)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:4)

你有没有在某处初始化ImageView?整个过程看起来都是空的。

我还建议使用BitmapFactory.decodeStream(),它返回一个Bitmap而不是Drawable。您还可以传递BitmapFactory.Options,它允许您使用更改输入样本大小和其他一些有用的选项。

答案 1 :(得分:1)

您的ImageView在imageView.setImageDrawable(result);

时为空