java - Save Image Byte Array to .net webservice and retrieve it -


i have .net asmx webservice i'm consuming using ksoap2 library. in service, first save user image , later retrieve it. however, once retrieve it, byte array intact, bitmapfactory unable decode , returns null.

to convert byte array:

bitmap viewbitmap = bitmap.createbitmap(imageview.getwidth(), imageview.getheight(), bitmap.config.argb_8888); bytearrayoutputstream bos = new bytearrayoutputstream(); viewbitmap.compress(compressformat.png, 0 /* ignored png */, bos); byte[] bitmapdata = bos.tobytearray(); 

the webservice accepts bytearray in byte[] format.

to convert array bitmap:

byte[] blob= info.get(main.key_thumb_bytes).getbytes(); bitmap bmp=bitmapfactory.decodebytearray(blob,0,blob.length); // return null :( imageview.setimagebitmap(bmp); 

from partial-analysis, appears byte array not change. why decoding return null? there better save image , pass through webservice? didn't analyze whole byte array, i'm guessing might've changed bit.

any thoughts? many thanks!

update: tried converting byte[] string using:

base64.encodetostring( bos.tobytearray(), base64.default); 

and decode using:

byte[] blob= base64.decode(info.get(main.key_thumb_bytes)); 

now white picture. i'm not sure what's wrong here. please help.

update: i'm storing image inside of database, in column of type varchar(max). should storing byte array string inside different sql data type? i'm not experienced sql, used varchar because did not convert text unicode, thought might thie byte array.

thanks!

convert byte arrays base64 string , easy transfer:

public static string bitmaptobase64(bitmap bitmap) {         byte[] bitmapdata = bitmaptobytearray(bitmap);         return base64.encodebytes(bitmapdata);     }      public static byte[] bitmaptobytearray(bitmap bitmap) {         bytearrayoutputstream bos = new bytearrayoutputstream();         bitmap.compress(compressformat.png, 0 /* ignored png */, bos);         byte[] bitmapdata = bos.tobytearray();         return bitmapdata;     } 

and

public static bitmap base64tobitmap(string strbase64) throws ioexception {     byte[] bitmapdata = base64.decode(strbase64);     bitmap bitmap = bitmapfactory.decodebytearray(bitmapdata, 0,             bitmapdata.length);     return bitmap; } 

also can not on image files on every file types:

public static string filetobase64(string path) throws ioexception {     byte[] bytes = filetobytearray(path);     return base64.encodebytes(bytes); }  public static byte[] filetobytearray(string path) throws ioexception {     file imagefile = new file(path);     byte[] data = new byte[(int) imagefile.length()];     fileinputstream fis = new fileinputstream(imagefile);     fis.read(data);     fis.close();     return data; }   public static void base64tofile(string path, string strbase64)         throws ioexception {     byte[] bytes = base64.decode(strbase64);     bytearraytofile(path, bytes); }  public static void bytearraytofile(string path, byte[] bytes)         throws ioexception {     file imagefile = new file(path);     file dir = new file(imagefile.getparent());     if (!dir.exists()) {         dir.mkdirs();     }     fileoutputstream fos = new fileoutputstream(imagefile);     fos.write(bytes);     fos.close(); } 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -