Image Not Showing On Image View
I am trying to set image on imageview but image is not show. I am reading image url from json data and then trying to set it on ImageView but my image is not visible. No any
Solution 1:
Please Use below code for get image from url and display into imageview.
publicclassimageextendsActivity {
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmapbitmap= DownloadImage("http://www.gophoto.it/view.php?i=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_KK4poNaL7XzYaizHU4faYysXvLc3rDSNRCME5hjdr3bMnNzefMmHOi-GuaSONhixPE34YtlICE7RVWEN55W7r2l2fs8zGCpdVJPe7OoEqY74Iaq5IO6D20W-Pl99h_RVElxSjtwIGrx9/s1600/Sachin+Tendulkar.png");
RelativeLayoutmRlayout1= (RelativeLayout) findViewById(R.id.mRlayout1);
Drawable d=newBitmapDrawable(bitmap);
mRlayoutLogin.setBackgroundDrawable(d);
}
private InputStream OpenHttpConnection(String urlString)throws IOException {
InputStreamin=null;
intresponse= -1;
URLurl=newURL(urlString);
URLConnectionconn= url.openConnection();
if (!(conn instanceof HttpURLConnection))
thrownewIOException("Not an HTTP connection");
try {
HttpURLConnectionhttpConn= (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
thrownewIOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmapbitmap=null;
InputStreamin=null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
}
Solution 2:
you can view image by using this code.
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Solution 3:
It seems you are downloading the image from UI thread. this will block the UI thread and will give you not responding error. as an easy way, you can use a library like Universal Image Loader
Universal Image Loader - GitHub
this will manage the image loading for you and avoid problems like incorrect urls, Out Of Memory error.
Post a Comment for "Image Not Showing On Image View"