尝试使用位图从 Url 设置 ImageView

Trying to set ImageView from Url with Bitmap

我正在尝试设置我的 ImageView.I 具有图像的 url 并尝试将其设为位图,然后将此位图设置为我的 ImageView.However,位图结果,参数onPostExecute,来自 download_Image 函数,为 null。意思是' BitmapFactory.decodeStream(is); ' 正在返回 null。

this.ImageView1 = (ImageView) infoWindow.findViewById(R.id.ImageView1);
ImageView1.setTag(URL);

new AsyncTask<ImageView, Void, Bitmap>(){
 ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(ImageView... imageViews) {
      final Bitmap[] b = new Bitmap[1];
      this.imageView = imageViews[0];

      activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
          b[0] =download_Image((String) imageView.getTag());
        }
      });

    return b[0];

    }

    @Override
    protected void onPostExecute(Bitmap result) {
      imageView.setImageBitmap(result);
    }

    public Bitmap download_Image(String url) {

    //---------------------------------------------------

      URL newurl = null;

      try {
        newurl = new URL(url);
      } catch (MalformedURLException e) {
         e.printStackTrace();
      }

      Bitmap mIcon_val = null;

      try {
        URLConnection urlConnection=newurl.openConnection();
        InputStream is=urlConnection.getInputStream();
        mIcon_val=BitmapFactory.decodeStream(is);
      } catch (Exception e) {
        e.printStackTrace();
      }

     return mIcon_val;

     //---------------------------------------------------

    }

}.execute(ImageView1);

我该如何处理这个问题?

在您的 gradle 依赖项中写入此内容

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0'
}

在你的活动中

Glide.with(activity.this).load(your image url).into(imageView);

如果有任何问题,请使用此代码并发表评论`public class MainActivity extends AppCompatActivity {

private ImageView imageView;
private String imageUrl ="image url";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView = (ImageView)findViewById(R.id.imageview);

    new AsyncTask<ImageView, Void, Bitmap>(){
        ImageView imageView = null;

        @Override
        protected Bitmap doInBackground(ImageView... imageViews) {
            final Bitmap[] b = new Bitmap[1];
            this.imageView = imageViews[0];

                    b[0] =download_Image(imageUrl);

           return b[0];

        }

        @Override
        protected void onPostExecute(Bitmap result) {
            imageView.setImageBitmap(result);
        }

        public Bitmap download_Image(String url) {

            //---------------------------------------------------

            URL newurl = null;

            try {
                newurl = new URL(url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            Bitmap mIcon_val = null;

            try {
                URLConnection urlConnection=newurl.openConnection();
                InputStream is=urlConnection.getInputStream();
                mIcon_val= BitmapFactory.decodeStream(is);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return mIcon_val;

            //---------------------------------------------------

        }

    }.execute(imageView);}}`