android - Downloading the image using OkHttp gives null -
i tried download image url using okhttp . returns null.
i have permissions
<uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_network_state"/>
here rest client
public class restclient { private static volatile restclient instance; private final okhttpclient client; private restclient(context context) throws exception { client = new okhttpclient(); } public static restclient getinstance(context context) throws exception { if (instance == null) synchronized (restclient.class) { if (instance == null) instance = new restclient(context); } return instance; } public call uploadpicture(callback callback){ string link = "http://www.101apps.co.za/images/headers/101_logo_very_small.jpg"; request request = new request.builder() .url(string.format(link)) .get() .build(); call call = client.newcall(request); call.enqueue(callback); return call; } }
here getpicture method in main activity
public void getpicbyurl() { try { restclient.getinstance(getactivity()).uploadpicture(new callback() { @override public void onfailure(request request, ioexception e) { e.printstacktrace(); } @override public void onresponse(response response) throws ioexception { final string json = response.body().string(); if (response.code() == constants.successful_download) { responsebody in = response.body(); inputstream inputstream = in.bytestream(); bufferedinputstream bufferedinputstream = new bufferedinputstream(inputstream); updateimage(bitmapfactory.decodestream(bufferedinputstream)); } } }); } catch (exception e) { e.printstacktrace(); } } private void updateimage(final bitmap bitmap) { new handler(looper.getmainlooper()).post(new runnable() { @override public void run() { if (bitmap != null) { mselectedpic.setimagebitmap(bitmap); } else { toast.maketext(getactivity(), r.string.show_error, toast.length_short).show(); } } }); }
updateimage()
method takes bitmap = null. don`t know doing wrong. please help.
with current url (http://www.101apps.co.za/images/headers/101_logo_very_small.jpg
) , purpose (loading image imageview only), suggest use picasso or glide instead.
sample code if using picasso:
imageview imageview = (imageview) findviewbyid(r.id.imageview); picasso.with(this) .load("http://www.101apps.co.za/images/headers/101_logo_very_small.jpg") .into(imageview);
hope helps!
Comments
Post a Comment