Reputation: 3539
I am trying to download an image in the background so that the app does not hang while the image is downloading. I am getting a nullpointerexception
and I don't understand what I'm doing wrong. My logs say that I'm grabbing data from the URL as I expect to, so I'm not sure exactly why this isn't working. Thanks!
Here is the code that I have:
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;
}
}
}
Here is the error:
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)
Upvotes: 0
Views: 2580
Reputation: 2629
Have you initialized the ImageView somewhere? Looks null the whole way through to me.
I would also recommend the use of BitmapFactory.decodeStream() which returns a Bitmap instead of a Drawable. You can also pass a BitmapFactory.Options which allows use to change the input sample size and several other useful options.
Upvotes: 4