multithreading - Why am I getting null pointer in the getGraphObject when trying to post a image to facebook from android? -
i trying post photo url facebook. so, have method converts image located in provided url bitmap, after want publish image facebook photos of user. code:
method converts image located in url bitmap image:
public bitmap downloadimage(string url) { bitmap bm = null; try { url aurl = new url(url); urlconnection conn = aurl.openconnection(); conn.connect(); inputstream = conn.getinputstream(); bufferedinputstream bis = new bufferedinputstream(is); bm = bitmapfactory.decodestream(bis); bis.close(); is.close(); } catch (ioexception e) { log.e("hub","error getting image server : " + e.getmessage().tostring()); } return bm; } this try post image in facebook:
private void posttofacebook(byte[] data) { session session = session.getactivesession(); if (session != null) { bundle postparams = new bundle(); postparams.putstring("caption", "teste caption"); postparams.putbytearray("picture", data); request.callback callback = new request.callback() { public void oncompleted(response response) { jsonobject graphresponse = response.getgraphobject().getinnerjsonobject(); string postid = null; try { postid = graphresponse.getstring("id"); } catch (jsonexception e) { log.i("", "json error "+ e.getmessage()); } facebookrequesterror error = response.geterror(); if (error != null) { toast.maketext(getcontext(), error.geterrormessage(), toast.length_short).show(); } else { toast.maketext(getcontext(), postid, toast.length_long).show(); } } }; request request = new request(session, "me/photos", postparams, httpmethod.post, callback); requestasynctask task = new requestasynctask(request); task.execute(); } else { toast.maketext(getcontext(), "not logged", toast.length_short).show(); } but @ line: response.getgraphobject(), getting null pointer exception. i've tried execute in background thread, this:
class convertimagetask extends asynctask<url, integer, byte[]> { asynctask<params,progress,result> @override protected void onpreexecute() { super.onpreexecute(); // nothing here } @override protected byte[] doinbackground(url... params) { byte[] data; bitmap bi = downloadimage(getphotourl()); bytearrayoutputstream baos = new bytearrayoutputstream(); bi.compress(bitmap.compressformat.png, 100, baos); data = baos.tobytearray(); return data; } @override protected void onpostexecute(byte[] result) { posttofacebook(result); } but error continues.
how can fix this? thank you!
the pages i've gone through show "picture" parameter url, not data can upload computer.
to upload photo using:
request request = request.newuploadphotorequest(session.getactivesession(), bitmapfactory.decodestream(this.getassets().open("yourfile.png")), new request.callback(){ @override public void oncompleted(response response) { facebookrequesterror error = response.geterror(); if(error != null){ log.i(this.getclass().getname(),"error publishing."); }else{ jsonobject graphresponse = response.getgraphobject().getinnerjsonobject(); string postid = null; try { postid = graphresponse.getstring("id"); } catch (jsonexception e) { log.i("facebook error", "json error " + e.getmessage()); } //logged in succesfully! } } }); bundle params = request.getparameters(); params.putstring("name", "the title photo."); request.setparameters(params); request.executeasync(); i passing png have in assets folder, can adapt part need. looking @ code should straight forward.
Comments
Post a Comment