Bitmapfactory.decodebytearray () returns NULL, parsing and resolving

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.