简体   繁体   English

在背景android中下载图片

[英]Download image in background android

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. 我收到一个nullpointerexception ,但我不明白我在做什么错。 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. 我的日志说我正在按期望的方式从URL抓取数据,因此我不确定这为什么不起作用。 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)

Have you initialized the ImageView somewhere? 您是否在某处初始化了ImageView? 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. 我还建议使用BitmapFactory.decodeStream() ,它返回一个位图而不是一个Drawable。 You can also pass a BitmapFactory.Options which allows use to change the input sample size and several other useful options. 您还可以传递一个BitmapFactory.Options ,它可以用来更改输入样本大小和其他几个有用的选项。

您的ImageView在imageView.setImageDrawable(result);处为null imageView.setImageDrawable(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM