problem Description: get pictures from Android camera, upload to remote database (MySQL), store in BLOB format,
However, when extracting pictures, it is always impossible to display them in the Android interface, the sample code is as follows:
..... ....
s = new Socket ("192.168.0.68", 9999);
DIN = new DataInputStream (S.getinputstream ());
... ...
int size = Din.readint ();//Read image data from server side
Byte[] bs = new byte[size];
Din.read (BS);
Bitmap B = Bitmapfactory.decodebytearray (bs,0,bs.length);
LOG.I ("kkk00", string.valueof (b));//output test,bitmap always null
Workaround:
Method 1: increased transcoding processing
int size = Din.readint ();//Read image data from server side
Byte[] bs = new byte[size];
Din.read (BS);
Yuvimage yuvimage=new yuvimage (BS, IMAGEFORMAT.NV21, 20,20, null);//20, 20 are the width and height of the graph respectively
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Yuvimage.compresstojpeg (New Rect (0, 0,20, a), BAOs),//80--jpg image quality [0-100],100 highest
byte[] Jdata = Baos.tobytearray ();
Bitmap bmp = Bitmapfactory.decodebytearray (jdata, 0, jdata.length);
Test: BMP is not empty, put into the control can be displayed normally.
Method 2: When the picture is transmitted to the server side in binary form, it is stored on the local computer and deposited in the MySQL database in the form of a file stream
.......
int size = Din.readint ();//Read the length of the image array
FileOutputStream fos = new FileOutputStream ("C:/bbbb1.bmp");
int len=0;
Byte[] Mm=new byte[1024];
while (true) {
int M=din.read (mm);
Len=len+m;
Fos.write (MM,0,M);
if (len>=size) break;
}
Fos.close ();
Write to MySQL in read file
..... ...
File File = new file ("C:/bbbb1.bmp");
int length = (int) file.length ();
InputStream fin = new FileInputStream (file);
Pstmt.setbinarystream (9, fin, length);
Bitmapfactory.decodebytearray () returns NULL, parsing and resolving