Android - BitmapFactory.decodeStream()始终返回null

时间:2014-03-22 23:26:51

标签: android bitmapfactory

我正在尝试从互联网下载图片以在Android应用中显示。我正在使用AsyncTask来避免NetworkOnMainThreadException,但无论我做什么,BitmapFactory.decodeStream()总是返回null:

这是我尝试过的方法之一:

protected Bitmap doInBackground(String... args) {

    String url = args[0];
    Bitmap bm = null;
    try {
        URL targetUrl = new URL(url);
        HttpURLConnection conn  = (HttpURLConnection) targetUrl.openConnection();
        InputStream is = conn.getInputStream();
        bm = BitmapFactory.decodeStream(is);
        is.reset();         // to allow reuse
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bm;
}

这也不起作用:

try {
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);
    HttpResponse response = client.execute(getRequest);
    final HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    bm = BitmapFactory.decodeStream(is);
    is.reset();
} catch (Exception e) {
    e.printStackTrace();
}
return bm;

**添加图片的最终方法**

protected void onPostExecute(Bitmap res) {
    img.setImageBitmap(res);    // img is an ImageView object for my Asynctask-extended class
}

logcat的

03-22 16:39:20.774: E/AndroidRuntime(22418): FATAL EXCEPTION: main
03-22 16:39:20.774: E/AndroidRuntime(22418): java.lang.NullPointerException
03-22 16:39:20.774: E/AndroidRuntime(22418):    at com.dannyc.facebooktestapp.FacebookFriendAdapter$ProfilePictureTask.onPostExecute(FacebookFriendAdapter.java:96)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at com.dannyc.facebooktestapp.FacebookFriendAdapter$ProfilePictureTask.onPostExecute(FacebookFriendAdapter.java:1)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at android.os.AsyncTask.finish(AsyncTask.java:631)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at android.os.Looper.loop(Looper.java:137)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at android.app.ActivityThread.main(ActivityThread.java:5104)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at java.lang.reflect.Method.invoke(Method.java:511)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-22 16:39:20.774: E/AndroidRuntime(22418):    at dalvik.system.NativeStart.main(Native Method)

我也尝试过BufferedInputStream,以及此处列出的其他答案,但都无济于事。这真让我疯了。我正在使用Android API等级8,如果有帮助的话。

有人有解决方案吗?

0 个答案:

没有答案