2

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

Community
  • 1
  • 1
user3358306
  • 187
  • 1
  • 13

6 Answers6

2

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);
Mugunthan S
  • 538
  • 3
  • 8
  • 21
1

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);
Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
  • 2
    Is there anything that easy just inside android? I wouldn't really want to use a library. – user3358306 Aug 23 '14 at 01:24
  • @user3358306 there is no other library is using in my posted answer.. Try it. – Priyanka Aug 23 '14 at 07:17
  • 1
    I have used Picasso in several projects and It is must have library for me. It handles caching (memory and disk cache), safe resizing/cropping, custom transformations (e.g.: circle image), works with adapters (listview...) also It can load local files (It handles also many OOM exceptions - by using fit() method...). – Yuraj Aug 23 '14 at 09:25
  • 1
    You can of course do it yourself, but you have to come up with solutions for resizing, caching, transforming or cropping the image, handling download redirects, http errors, etc. Picasso does all that for free and it's all well tested and maintained code. So it's a strong recommendation to use it instead of start writing it by yourself. – Alécio Carvalho Aug 23 '14 at 11:31
1

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.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

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

HukeLau_DABA
  • 2,384
  • 6
  • 33
  • 51
0
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.

Jossy Paul
  • 1,267
  • 14
  • 26
0

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 :)

Priyanka
  • 677
  • 5
  • 20