dimanche 1 mars 2015

Send image from c++ server to Android client

I have on my C++ server an image saved as blob in a sqlite database. I take these image from database like this:



int sizeForThumbnail = sqlite3_column_bytes(sqlStmt, 1);
char* thumbnail = (char * )sqlite3_column_blob(sqlStmt,1);


After this i send it to the Android phone using a socket connection:



strcpy(messageToSend,to_string(sizeForThumbnail));
write(clientDescriptor,messageToSend,strlen(messageToSend));
write(clientDescriptor,thumbnail,sizeForThumbnail);


On the Android phone i try to get the image like this:



BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
char[] buffer = new char[65536];
int countForSize = br.read(buffer,0,65536);
buffer[countForSize] = '\0';
String size = new String(buffer,0,countForSize);

InputStream in = socket.getInputStream();
int sizeInt = Integer.parseInt(size);
byte[] byteBuffer = new byte[sizeInt];
int countForBytes = in.read(byteBuffer,0,sizeInt);
Bitmap bmp = BitmapFactory.decodeByteArray(byteBuffer, 0, byteBuffer.length);
ImageView photo = (ImageView) findViewById(R.id.imageView);
photo.setImageBitmap(bitmap);


Now i read exactly how much i send from the c++ server. countForBytes is equal with sizeForThumbnail but decodeByteArray() returns null. What am i doing wrong? After i get the image from database i tried to write thumbnail in a file and save it as jpeg format and it gets the correct image so the retrieval from the database is correct.


Aucun commentaire:

Enregistrer un commentaire