Reputation: 187
I'm trying to use the image from this url: http://www.bisonsoftware.us/hhs/image/1.png
I want to take this image and set it in a imageview, I'm just looking for the easiest way to do this.
I tried this solution but it seemed to not work for me, it gave an error saying malformedurlexception: Load image from url
Upvotes: 2
Views: 976
Reputation: 538
Use the universal image library:
After adding this library with your project. write this code on necessary place
ImageLoader imageloader;
DisplayImageOptions options;
imageloader = ImageLoader.getInstance();
String url="http://www.bisonsoftware.us/hhs/image/1.png";
imageloader.init(ImageLoaderConfiguration.createDefault(getApplicationContext()));
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.resetViewBeforeLoading(true).cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true)
.displayer(new FadeInBitmapDisplayer(300)).build();
imageloader.displayImage(url,imageview);
Upvotes: 2
Reputation: 675
Try to implement like this. Its working for me for download image from url.
And also don't forget to add Internet permission in manifest file,like this-<uses-permission android:name="android.permission.INTERNET"/>
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
ProgressDialog progressDialog;
ImageView imageView;
Bitmap map = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
new loadImage().execute();
}
public class loadImage extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
map = downloadImage("http://www.bisonsoftware.us/hhs/image/1.png");
return null;
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
imageView.setImageBitmap(map);
}
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
stream.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString) throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
@Override
public void onDestroy() {
super.onDestroy();
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.cancel();
}
}
@Override
protected void onStop() {
super.onStop();
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}
}
Happy Coding :)
Upvotes: 0
Reputation: 1287
public class MainActivity extends Activity {
private Bitmap bitmap;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageView1);
new downloadimage().execute();
}
private class downloadimage extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params) {
URL url;
try {
url = new URL("http://www.bisonsoftware.us/hhs/image/1.png");
bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
imageView.setImageBitmap(bitmap);
}
}
}
This is working for me.
Upvotes: 0
Reputation: 26017
If you just want to use native android code, you can download it using AsyncTask
and then set it in onPostExecute
.
Call it as:
//pass image view while creating AskyncTask object and pass url as parameter
new DownloadImageTask((ImageView) findViewById(R.id.yourImageView)).execute("your_url_here");
AsynTask to call:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
I just saw that a similar answer is also mentioned in the url which you've mentioned in the question. This is working for my app. If its gives MalformedUrlException
, then you can try checking if url is proper and if needed encode it properly.
Upvotes: 1
Reputation: 2528
You can do it the hard way like Shobhit described, or there's a library that does the same thing(creates another thread to render the image) for you such as the aforementioned picasso or UniversalImageLoader: https://github.com/nostra13/Android-Universal-Image-Loader
Upvotes: 0
Reputation: 13657
Use the Picasso library https://square.github.io/picasso/
It would be just like this:
Picasso.with(context).load("http://www.bisonsoftware.us/hhs/image/1.png").into(imageView);
Upvotes: 1