Android Image View From Url
I am downloading an image from url, but the image is not changed after the download is complete. I am entering code below, anybody experienced the same? Java file public class MyIm
Solution 1:
Please Use below code for download and display image 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/AVvXsEjat4DPlVzROqw45aAF_WXGyNrAbQbm1mT06hC7z1gOZ7hPt9TgFMo8hSLV2Vf2ILWBtWOpHiCVjUNMuOmcHd23XhqxBOyrQGlyjMLE5P9KVAzCGOpELrc_jV71jTd4VaaClMJGcFOxmAnj/s1600/Sachin+Tendulkar.png");
ImageViewimg= (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
}
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:
Following code working with the following url but its not working with your url.the problem is with your image size.Try with another url it will work.
publicclassMyImgActivityextendsActivity {
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageViewimgView=(ImageView)findViewById(R.id.imageView1);
URLurl=null;
Bitmapbmp=null;
try {
url = newURL("http://www.seobook.com/images/smallfish.jpg");
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
}catch (IOException e) {
}
imgView.setImageBitmap(bmp);
}
}
Solution 3:
try
{
URLmurl=newURL(url)
URLConnectionucon= murl.openConnection();
InputStreamis= ucon.getInputStream();
Drawabled= Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
returnnull;
}
use this cose inside your download method and if the connection speed is slow use thread to donwload and hanlder to post the image...as explained by @Hiren
Solution 4:
try below code,
just change your URL to,
onCreate
newThread(newRunnable() {
@Overridepublicvoidrun() {
// TODO Auto-generated method stub
drawable = LoadImageFromWebOperations("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjat4DPlVzROqw45aAF_WXGyNrAbQbm1mT06hC7z1gOZ7hPt9TgFMo8hSLV2Vf2ILWBtWOpHiCVjUNMuOmcHd23XhqxBOyrQGlyjMLE5P9KVAzCGOpELrc_jV71jTd4VaaClMJGcFOxmAnj/s1600/Sachin+Tendulkar.png");
handler.sendEmptyMessage(0);
}
}).start();
set image after downloading image using handler
Handlerhandler=newHandler(){
@OverridepublicvoidhandleMessage(android.os.Message msg) {
imgView.setImageDrawable(drawable);
Log.i("System out","after set the image...");
}
};
Hope help you...
Post a Comment for "Android Image View From Url"