我正在尝试在后台下载图像,以便在下载图像时应用程序不会挂起。我得到一个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)
最佳答案
您是否在某处初始化了ImageView?整个过程看起来都是零。
我还建议使用BitmapFactory.decodeStream(),它返回位图而不是Drawable。您还可以传递BitmapFactory.Options,该键可用于更改输入样本大小和其他几个有用的选项。