java - Couchdb Upload Image via Httpclient -
i build android app couchdb, tried uploaded image couchdb document function:
public jsonobject uploadpicture(putattachment putattachment) { jsonobject obj = null; try { httpclient httpclient = new defaulthttpclient(); httpput httpput = new httpput(baseurl() + putattachment.getdbname() + "/" + putattachment.getdocname() + "/attachment?rev=" + putattachment.getrev()); bytearrayentity img = new bytearrayentity(putattachment.getbyteimg()); httpput.setentity(img); httpput.setheader("content-length", "" + (int) img.getcontentlength()); httpput.setheader("content-type", "image/png"); httpput.setheader(authenticate()); httpresponse response; response = httpclient.execute(httpput); httpentity entity = response.getentity(); if (entity != null) { inputstream instream = entity.getcontent(); obj = new jsonobject(convertstreamtostring(instream)); instream.close(); } } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } return obj; } and don't know why every time clientprotocolexception
after
httpclient.execute(httpput). someone know
i struggling today. after studying this: how put image attachment couchdb in android?
i got in end:
public static httpresponse makeupdaterequest(string uri, bitmap bmp) { try { httpput httpput = new httpput(uri); bytearrayoutputstream stream = new bytearrayoutputstream(); bmp.compress(bitmap.compressformat.png, 0, stream); bytearrayentity entity = new bytearrayentity(stream.tobytearray()); entity.setcontenttype("image/png"); entity.setchunked(true); httpput.setentity(entity); return new defaulthttpclient().execute(httpput); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; } and called in floowing way:
httpresponse updateresponse = makeupdaterequest( appconfig.web_server_db_uri + uuid + "/attachment?rev=" + revid, bmp); this reading: http://wiki.apache.org/couchdb/http_document_api#inline_attachments
Comments
Post a Comment