How To Set A Image To A Image View From A Url Android
Solution 1:
You are running the network related operation on the ui thread. So, you get NetworkOnMainThreadException
.
Bitmapbmp= BitmapFactory.decodeStream(url.openConnection().getInputStream());
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
You should use a Thread
or Asynctask
.
http://developer.android.com/reference/android/os/AsyncTask.html.
Check the docs there is an example.
Example:
publicclassMainActivityextendsActivity {
ImageView iv ;
ProgressDialog pd;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pd = newProgressDialog(this);
pd.setMessage("Downloading Image");
iv = (ImageView) findViewById(R.id.imageView1);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(newOnClickListener()
{
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stubnewDownloadImageTask(iv).execute("http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png");
}
});
}
classDownloadImageTaskextendsAsyncTask<String, Void, Bitmap> {
ImageView bmImage;
publicDownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
@OverrideprotectedvoidonPreExecute() {
// TODO Auto-generated method stubsuper.onPreExecute();
pd.show();
}
protectedBitmapdoInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStreamin = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
@OverrideprotectedvoidonPostExecute(Bitmap result) {
super.onPostExecute(result);
pd.dismiss();
bmImage.setImageBitmap(result);
}
}
}
activity_main.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:android1="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><ImageViewandroid:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="171dp"android:src="@drawable/ic_launcher" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="78dp"android:text="Button" /></RelativeLayout>
Snap
Solution 2:
You're getting NetworkOnMainThreadException
which indicates that you're performing network operation on main thread, while it should be performed in other thread.
Follow this question for further information: How to fix android.os.NetworkOnMainThreadException?
Solution 3:
As you are running Network Related work on MainThread
i.e. UIThread
which cause UIThread
not to laid out it's View
on Screen or Activity
. That's according to ThreadPolicy
all the time consuming and variable operation are need to performed on AysncTask
or Thread
.
As per below LongOperation
is AsyncTask
which can be executed by calling execute()
on it.
new LongOperation().execute();
privateclassLongOperationextendsAsyncTask<String, Void, Bitmap> {
@OverrideprotectedStringdoInBackground(Bitmap... params)
{
try
{
URL url = newURL(parsedWeatherResponse.getWeatherIconUrl());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmp;
}
@OverrideprotectedvoidonPostExecute(Bitmap bmp)
{
super.onPostExecute(result);
weather_image.setImageBitmap(bmp);
}
@OverrideprotectedvoidonPreExecute() {
}
@OverrideprotectedvoidonProgressUpdate(Void... values) {
}
}
Solution 4:
You can directly show image from web without downloading it. Please check the below function . It will show the images from the web into your image view.
publicstatic Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
returnnull;
}
}
then set image to imageview using code in your activity.
Post a Comment for "How To Set A Image To A Image View From A Url Android"