got null when creating bitmap from byte array

Viewed 402

I create a main HASHMAP repo for caching user images that comes from the server. this cache is static. this cache saves user id as a key and byte array as a value.

private static HashMap<String, byte[]> sDrawables;

public static HashMap<String, byte[]> getMapInstance (){
  if (sDrawables == null)
     sDrawables = new HashMap<>();

  return sDrawables;
}

I use of AsyncTask for downloading images from the server and put this id and images array on hashmap and after this, I put this map to my main static hashmap:

for (Map.Entry<String, byte[]> entry : drawables.entrySet()) {
  ImgFactory.getMapInstance().put(entry.getKey(), entry.getValue());
}

On the place that I want to set this user image to my imageview, I send user Id to the main hashmap and after finding it I get its value and I convert this value to a bitmap. but bitmap it is not created my bitmap is null:

public static Drawable getConfigDrawable(Context context, String name) {
  if (getMapInstance() != null && getMapInstance().containsKey(name)) {
     Log.i("====>", "getConfigDrawable: "+ getMapInstance().containsKey(name));
     byte[] arr = getMapInstance().get(name);
     Bitmap bitmap = BitmapFactory.decodeByteArray(arr, 0, arr.length);

     Log.i("===", "getConfigDrawable: " + bitmap.getHeight());
     return new BitmapDrawable(context.getResources(), bitmap);
  }
  return null;
}

The message

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getHeight()' on a null object reference
                                                                               at safarayaneh.com.anotify.util.ImgFactory.getConfigDrawable(ImgFactory.java:72)
                                                                               at safarayaneh.com.anotify.adapter.NotifyAdapter$1.onComplete(NotifyAdapter.java:201)
                                                                               at safarayaneh.com.anotify.util.ImgFactory$1.onResult(ImgFactory.java:59)
                                                                               at safarayaneh.com.anotify.tasks.GetImageTask.onPostExecute(GetImageTask.java:87)
                                                                               at safarayaneh.com.anotify.tasks.GetImageTask.onPostExecute(GetImageTask.java:21)
                                                                               at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                               at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                               at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

My log:

anotify.debug I/====>: getConfigDrawable: true


Log.i("=====", "getConfigDrawable: length:"+ arr.length);
debug I/=====: getConfigDrawable: length: 12090

EDIT

Finally, these code was ok and I got a bitmap.

     byte[] tmp=new byte[result.getJSONArray("UserImage").length()];
        for(int i=0;i<result.getJSONArray("UserImage").length();i++){
            tmp[i]=(byte)(((int)result.getJSONArray("UserImage").get(i)) & 0xFF);
        }
1 Answers
Related