how to set image from url for imageView
Solution 1
EDIT:
Create a class that extends AsyncTask
public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {
private String url;
private ImageView imageView;
public ImageLoadTask(String url, ImageView imageView) {
this.url = url;
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
URL urlConnection = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlConnection
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
imageView.setImageBitmap(result);
}
}
And call this like new ImageLoadTask(url, imageView).execute();
Direct method:
Use this method and pass your url as string. It returns a bitmap. Set the bitmap to your ImageView.
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
And then this to ImageView like so:
imageView.setImageBitmap(getBitmapFromURL(url));
And dont forget about this permission in maifest.
<uses-permission android:name="android.permission.INTERNET" />
NOTE:
Try to call this method from another thread or AsyncTask because we are performing networking operations.
Solution 2
You can also let Square's Picasso library do the heavy lifting:
Picasso
.get()
.load("http://...")
.into(imageView);
As a bonus, you get caching, transformations, and more.
Solution 3
Try:
URL newurl = new URL(photo_url_str);
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
profile_photo.setImageBitmap(mIcon_val);
More from
1) how-to-load-an-imageview-by-url-in-android.
2) android-make-an-image-at-a-url-equal-to-imageviews-image
Solution 4
Using Glide library:
Glide.with(context)
.load(new File(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
Solution 5
easy way to use Aquery library it helps to get direct load image from url
AQuery aq=new AQuery(this); // intsialze aquery
aq.id(R.id.ImageView).image("http://www.vikispot.com/z/images/vikispot/android-w.png");
Related videos on Youtube
Comments
-
Shanaz K over 2 years
I wanna set Image in ImageView using Url for example I have this url
but there is no option to set url
-
Tushar Nallan about 9 yearsIn 1st method, instead of setting imageView directly from onPostExecute, use imageView.post(new Runnable() { public void run() { imageView.setImageBitmap(result); } });
-
kalyan pvs about 9 years@Tushar its not needed..onpostExecute() will call in UI thread.
-
sam almost 7 yearsWorks like Gem!
-
FuriosoJack over 6 yearsEres un crack, te mereces todo Jajajaj
-
Tarit Ray almost 6 yearsBut it work for .png type image url, it didn't worked for .svg url or for any other url to me . If there is any work around kindly share !!!
-
S Kumar about 4 yearsFor Direct Method you need to add this Code in
onCreate
method, Code Mentioned bellowStrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
-
Mattwmaster58 about 2 yearsDoes this block the main/UI thread? Java noob here
-
ʀᴀʜɪʟ about 2 yearsYep, it will. Make sense if the image size is large.