Send image data to android app from App Engine -
in app engine backend have method gets image google cloud storage
@apimethod( name = "getprofileimage", path = "image", httpmethod = apimethod.httpmethod.get) public image getprofileimage(@named("imagename")string imagename){ try{ httptransport httptransport = googlenethttptransport.newtrustedtransport(); googlecredential credential = googlecredential.getapplicationdefault(); storage.builder storagebuilder = new storage.builder(httptransport,new jacksonfactory(),credential); storage storage = storagebuilder.build(); storage.objects.get getobject = storage.objects().get("mybucket", imagename); bytearrayoutputstream out = new bytearrayoutputstream(); // if you're not in appengine, download whole thing in 1 request, if possible. getobject.getmediahttpdownloader().setdirectdownloadenabled(false); getobject.executemediaanddownloadto(out); byte[] oldimagedata = out.tobytearray(); out.close(); imagesservice imagesservice = imagesservicefactory.getimagesservice(); return imagesservicefactory.makeimage(oldimagedata); }catch(exception e){ logger.info("error getting image named "+imagename); } return null; }
the issue having how image data when call in android app?
since cant return primitives app engine converted image
call getimagedata()
in app byte[].
however image object returned app not same 1 in app engine there no getimagedata().
how can image data android app?
if create object had byte[] variable in set byte[] variable string data , return object method work?
update
the image gets sent android app. (this code may or may not correct, have not debugged yet)
@workerthread public string startresumablesession(){ try{ file file = new file(mfilepath); long filesize = file.length(); file = null; string surl = "https://www.googleapis.com/upload/storage/v1/b/lsimages/o?uploadtype=resumable&name="+mimgname; url url = new url(surl); httpurlconnection urlconnection = (httpurlconnection)url.openconnection(); urlconnection.setrequestproperty("authorization",""); urlconnection.setrequestproperty("x-upload-content-type","image/png"); urlconnection.setrequestproperty("x-upload-content-length",string.valueof(filesize)); urlconnection.setrequestmethod("post"); if(urlconnection.getresponsecode() == httpurlconnection.http_ok){ return urlconnection.getheaderfield("location"); } }catch(exception e){ e.printstacktrace(); } return null; } private long sendnextchunk(string surl,file file,long skip){ int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 524287; long totalbytessent = 0; try{ long filesize = file.length(); fileinputstream fileinputstream = new fileinputstream(file); skip = fileinputstream.skip(skip); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); totalbytessent = skip + buffersize; buffer = new byte[buffersize]; bytesread = fileinputstream.read(buffer, 0, buffersize); try { while (bytesread > 0) { try { url url = new url(surl); httpurlconnection urlconnection = (httpurlconnection)url.openconnection(); urlconnection.setdoinput(true); urlconnection.setdooutput(true); urlconnection.setusecaches(false); urlconnection.setchunkedstreamingmode(524287); urlconnection.setrequestmethod("post"); urlconnection.setrequestproperty("connection", "keep-alive"); urlconnection.setrequestproperty("content-type","image/png"); urlconnection.setrequestproperty("content-length",string.valueof(bytesread)); urlconnection.setrequestproperty("content-range", "bytes "+string.valueof(skip)+"-"+string.valueof(totalbytessent)+"/"+string.valueof(filesize)); dataoutputstream outputstream = new dataoutputstream(urlconnection.getoutputstream()); outputstream.write(buffer, 0, buffersize); int code = urlconnection.getresponsecode(); if(code == 308){ string range = urlconnection.getheaderfield("range"); return integer.parseint(range.split("-")[1]); }else if(code == httpurlconnection.http_created){ return -1; } outputstream.flush(); outputstream.close(); outputstream = null; } catch (outofmemoryerror e) { e.printstacktrace(); // response = "outofmemoryerror"; // return response; return -1; } fileinputstream.close(); } } catch (exception e) { e.printstacktrace(); // response = "error"; // return response; return -1; } }catch(exception e){ e.printstacktrace(); } return -1; }
edit 2:
apparently not clear people using endpoints in android app
what ended doing/finding out need call execute()
on api call endpoints , returns real data passed api
example
the api call returns image
public image getprofileimage(@named("id") long id, @named("imagename")string imagename){ try{ profilerecord pr = get(id); httptransport httptransport = googlenethttptransport.newtrustedtransport(); googlecredential credential = googlecredential.getapplicationdefault(); storage.builder storagebuilder = new storage.builder(httptransport,new jacksonfactory(),credential); storage storage = storagebuilder.build(); storage.objects.get getobject = storage.objects().get("mybucket", imagename); bytearrayoutputstream out = new bytearrayoutputstream(); // if you're not in appengine, download whole thing in 1 request, if possible. getobject.getmediahttpdownloader().setdirectdownloadenabled(false); getobject.executemediaanddownloadto(out); byte[] oldimagedata = out.tobytearray(); out.close(); return imagesservicefactory.makeimage(oldimagedata); }catch(exception e){ logger.info("error getting image named "+imagename); } return null; }
then on client side call it
image = pr.profileimage(id,"name.jpg").execute(); byte[] data = i.decodeimagedata();
Comments
Post a Comment