How to load an ImageView by URL in Android? [close

2018-12-31 00:27发布

How do you use an image referenced by URL in an ImageView?

23条回答
看淡一切
2楼-- · 2018-12-31 00:30

Anyway people ask my comment to post it as answer. i am posting.

URL newurl = new URL(photo_url_str); 
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
profile_photo.setImageBitmap(mIcon_val);

thanks.

查看更多
残风、尘缘若梦
3楼-- · 2018-12-31 00:30

Android Query can handle that for you and much more (like cache and loading progress).

Take a look at here.

I think is the best approach.

查看更多
春风洒进眼中
4楼-- · 2018-12-31 00:32

This code is tested, it is completely working.

URL req = new URL(
"http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"
);
Bitmap mIcon_val = BitmapFactory.decodeStream(req.openConnection()
                  .getInputStream());
查看更多
旧时光的记忆
5楼-- · 2018-12-31 00:33

The accepted answer above is great if you are loading the image based on a button click, however if you are doing it in a new activity it freezes up the UI for a second or two. Looking around I found that a simple asynctask eliminated this problem.

To use an asynctask add this class at the end of your activity:

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);
    }    
}

And call from your onCreate() method using:

new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
        .execute(MY_URL_STRING);

The result is a quickly loaded activity and an imageview that shows up a split second later depending on the user's network speed.

查看更多
若你有天会懂
6楼-- · 2018-12-31 00:33

Lots of good info in here...I recently found a class called SmartImageView that seems to be working really well so far. Very easy to incorporate and use.

http://loopj.com/android-smart-image-view/

https://github.com/loopj/android-smart-image-view

UPDATE: I ended up writing a blog post about this, so check it out for help on using SmartImageView.

2ND UPDATE: I now always use Picasso for this (see above) and highly recommend it. :)

查看更多
孤独总比滥情好
7楼-- · 2018-12-31 00:34

You could also use this LoadingImageView view to load an image from a url:

http://blog.blundellapps.com/imageview-with-loading-spinner/

Once you have added the class file from that link you can instantiate a url image view:

in xml:

<com.blundell.tut.LoaderImageView
  android:id="@+id/loaderImageView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  image="http://developer.android.com/images/dialog_buttons.png"
 />

In code:

final LoaderImageView image = new LoaderImageView(this, "http://developer.android.com/images/dialog_buttons.png");

And update it using:

image.setImageDrawable("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
查看更多
登录 后发表回答