android - got null when creating bitmap from byte array -
i create main hashmap repo caching user images comes server. cache static. cache saves user id key , byte array 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 downloading images server , put id , images array on hashmap , after this, put map main static hashmap:
for (map.entry<string, byte[]> entry : drawables.entryset()) { imgfactory.getmapinstance().put(entry.getkey(), entry.getvalue()); }
on place want set user image imageview, send user id main hashmap , after finding value , convert value bitmap. bitmap not created bitmap 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 invoke virtual method 'int android.graphics.bitmap.getheight()' on null object reference @ safarayaneh.com.anotify.util.imgfactory.getconfigdrawable(imgfactory.java:72) @ safarayaneh.com.anotify.adapter.notifyadapter$1.oncomplete(notifyadapter.java:201) @ safarayaneh.com.anotify.util.imgfactory$1.onresult(imgfactory.java:59) @ safarayaneh.com.anotify.tasks.getimagetask.onpostexecute(getimagetask.java:87) @ safarayaneh.com.anotify.tasks.getimagetask.onpostexecute(getimagetask.java:21) @ android.os.asynctask.finish(asynctask.java:651) @ android.os.asynctask.-wrap1(asynctask.java) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:668) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ 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 ok , got 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); }
as getting byte asynktask should try decoding byte decodestream(inputstream is)
,decodestream , decode input stream bitmap instead of decodebytearray()
; decodebytearray , decode immutable bitmap specified byte array.
Comments
Post a Comment