android - Image Getter in Fragments -
i trying codes found here using imagegetter
display image source html code. encountered problem when app trying image said html code. first time working fragments.
this class working on extends fragment
. dont know how cast object result drawable
object inside fragment.
and enlighten me fragment
s, why codes work on normal activity
, when extend fragment need add getactivity
or getview
.
it error points out @ line: attempt invoke virtual object on null reference.
urldrawable.setbounds(0, 0, 0 + result.getintrinsicwidth(), 0 + result.getintrinsicheight());
logcat points error on code above result.getintrinsicwidth()
.
@override protected void onpostexecute(drawable result) { // set correct bound according result http call urldrawable.setbounds(0, 0, 0 + result.getintrinsicwidth(), 0 + result.getintrinsicheight()); // change reference of current drawable result // http call urldrawable.drawable = result; // redraw image invalidating container urlimageparser.this.container.invalidate(); }
do in background:
@override protected drawable doinbackground(string... params) { string source = params[0]; return fetchdrawable(source); }
whole class image getter:
public class imagegetterasynctask extends asynctask<string, void, drawable> { urldrawable urldrawable; public imagegetterasynctask(urldrawable d) { this.urldrawable = d; } @override protected drawable doinbackground(string... params) { string source = params[0]; return fetchdrawable(source); } @override protected void onpostexecute(drawable result) { // set correct bound according result http call urldrawable.setbounds(0, 0, 0 + result.getintrinsicwidth(), 0 + result.getintrinsicheight()); // change reference of current drawable result // http call urldrawable.drawable = result; // redraw image invalidating container urlimageparser.this.container.invalidate(); } /*** * drawable url * @param urlstring * @return */ public drawable fetchdrawable(string urlstring) { try { inputstream = fetch(urlstring); drawable drawable = drawable.createfromstream(is, "src"); drawable.setbounds(0, 0, 0 + drawable.getintrinsicwidth(), 0 + drawable.getintrinsicheight()); return drawable; } catch (exception e) { return null; } } private inputstream fetch(string urlstring) throws malformedurlexception, ioexception { defaulthttpclient httpclient = new defaulthttpclient(); httpget request = new httpget(urlstring); httpresponse response = httpclient.execute(request); return response.getentity().getcontent(); } }
Comments
Post a Comment