java - troubles with converting byte[] to bitmap (android) -
i saw various people got problems this. use retrofit 2.0.0 beta 2 library. in android application, want have profile picture (so have actions upload new image , see profile (with image). server asp.net web api.
i've been reading lot , far able upload image (not sure if correctly seems yes). have problems when trying image server display it, got answer server, when trying convert bytes array bitmap got null result.
here's code, u can check out:
android side (retrofit methods)
@post("userprofile/upload") call<responsebody> updateimage(@body requestbody file); @get("userprofile/getuserimage") call<responsebody> getuserimage();
usage of methods:
private void getprofilepic(){ call<responsebody> call = service.getuserimage(); call.enqueue(new customcallback<responsebody>(getactivity()) { @override public void onresponse(response<responsebody> response, retrofit retrofit) { if (response.issuccess()) { if (response.body() != null) { bitmap bm = null; try { bm = bitmapfactory.decodestream(response.body().bytestream()); } catch (ioexception e) { e.printstacktrace(); } profilepic.setimagebitmap(bm); dialog.dismiss(); } else { // todo } } else { // todo } } @override public void onfailure(throwable t) { super.onfailure(t); } }); public void oncropimageclick(view view) { bitmap cropped = mcropimageview.getcroppedimage(100, 100); if (cropped != null) { showloadingdialog(); bytearrayoutputstream stream = new bytearrayoutputstream(); cropped.compress(bitmap.compressformat.png, 100, stream); byte[] bytearray = stream.tobytearray(); requestbody requestbody = requestbody .create(mediatype.parse("application/octet-stream"), bytearray); call<responsebody> call = service.updateimage(requestbody); call.enqueue(new customcallback<responsebody>(this) { @override public void onsuccess(responsebody model) { dialog.dismiss(); finish(); } @override public void onfailure(throwable t) { super.onfailure(t); } }); } }
web api server side:
[authorize] [route("getuserimage")] [httpget] public async task<ihttpactionresult> getuserimage() { string username = user.identity.name; player player = await _service.findplayer(username); if (player == null) return badrequest(); return ok(player.userimage); } [authorize] [httppost] [route("upload")] public async task<ihttpactionresult> upload([nakedbody]byte[] file) { return await _service.tryupdateprofilepicture(file, user.identity.name) ? (ihttpactionresult)ok() : badrequest(); } public async task<bool> tryupdateprofilepicture(byte[] file, string username) { try { var player = await findplayer(username); player.userimage = file; _unitofwork.save(); return true; } catch (dbentityvalidationexception e) { return false; } }
what realized: when trying send bitmap (byte[]) server returns success , stored in database. when tryinng server got success response , have array of bytes when try cast bitmap got null object. also, wrote in comment section below, when fetch bytes server got longer array of bytes sent server, may weird, don't know.
Comments
Post a Comment