file upload - Android: Premature end of JPEG -


i have send server few jpeg files taken form camera. of course big file stream. code (for each file) looks follow:

struct3.put("type", "image/jpeg"); f = new file(filename); fileinputstream fis = new fileinputstream(f); bufferedinputstream bis = new bufferedinputstream(fis); byte[] buffer = new byte[(int)f.length()]; bis.read(buffer); fis.close(); struct3.put("bits", buffer); 

after send struct:

object[] params3 = { bid, login, pass, struct3 }; object response2 = client.send("my_function", params3); 

when send small files correct, when files bigger received "out of memory exception".

my solution of compress jpeg files:

struct3.put("type", "image/jpeg"); final options opts = new options(); opts.insamplesize = 2; bitmap bitmap = bitmapfactory.decodefile(filename, opts); bytearrayoutputstream stream = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.jpeg, 70, stream);  byte[] bytearray = stream.tobytearray(); struct3.put("bits", bytearray); object[] params3 = { bid, login, pass, struct3 }; object response2 = client.send("my_function", params3);     

but way produced error on server side: "premature end of jpeg file".

is there way correct jpeg file before sending it? know jpeg shoud end eoi ( 0xff, 0xfd).

how check , make corrections?

there no log cat report due photos uploaded wordpress , track warning gdlib. warning contains: "premature end of jpeg file"

although solved problem. i've implemented procedure checking if bytearray ends 0xff,0xd9 , in case not add 2 bytes:

bytearrayoutputstream stream = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.jpeg, 70, stream); byte[] bytearray = stream.tobytearray(); int bytetosendsize = bytearray.length; boolean proper = ((bytearray[bytearray.length-2])==((byte)0xff))      && ((bytearray[bytearray.length-1])==((byte)0xd9)); if(!proper) bytetosendsize +=2; byte[] bytetosend = new byte[bytetosendsize];  (int = 0; < bytearray.length; i++) { bytetosend[i] = bytearray[i]; } if(!proper){   bytetosend[bytearray.length] = (byte) 0xff;   bytetosend[bytearray.length+1] = (byte) 0xd9; } struct3.put("bits", bytetosend); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -