How to load an ImageView by URL in Android?

如何在ImageView使用URL引用的ImageView


From Android developer:

// show The Image in a ImageView
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
            .execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");

public void onClick(View v) {
    startActivity(new Intent(this, IndexActivity.class));
    finish();

}

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

Make sure you have the following permissions set in your AndroidManifest.xml to access the internet.

<uses-permission android:name="android.permission.INTERNET" />

You'll have to download the image firstly

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}

Then use the Imageview.setImageBitmap to set bitmap into the ImageView


1. Picasso allows for hassle-free image loading in your application—often in one line of code!

Use Gradle:

implementation 'com.squareup.picasso:picasso:2.71828'

Just one line of code!

Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);

Many common pitfalls of image loading on Android are handled automatically by Picasso

  • Handling ImageView recycling and download cancelation in an adapter.
  • Complex image transformations with minimal memory use.
  • Automatic memory and disk caching.
  • 在这里输入图像描述

    2. Glide An image loading and caching library for Android focused on smooth scrolling

    Use Gradle:

    repositories {
      mavenCentral() // jcenter() works as well because it pulls from Maven Central
    }
    
    dependencies {
      compile 'com.github.bumptech.glide:glide:3.7.0'
      compile 'com.android.support:support-v4:19.1.0'
    }
    

    // For a simple view:

    @Override public void onCreate(Bundle savedInstanceState) {
      ...
      ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
    
      Glide.with(this).load("http://i.imgur.com/DvpvklR.png").into(imageView);
    }
    

    // For a simple image list:

     @Override public View getView(int position, View recycled, ViewGroup container) {
          final ImageView myImageView;
          if (recycled == null) {
            myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false);
          } else {
            myImageView = (ImageView) recycled;
          }
    
          String url = myUrls.get(position);
    
          Glide
            .with(myFragment)
            .load(url)
            .centerCrop()
            .placeholder(R.drawable.loading_spinner)
            .crossFade()
            .into(myImageView);
    
          return myImageView;
    }
    
    链接地址: http://www.djcxy.com/p/31444.html

    上一篇: 如何在特定的LinearLayout中添加Canvas?

    下一篇: 如何在Android中通过URL加载ImageView?