How to get image from url website in imageview in android

前端 未结 4 1599
一向
一向 2021-01-12 10:19

I am trying to get image in ImageView from url website but the image not show so, What is the wrong in this code? This is the url of the image.

It is my Main Activi

4条回答
  •  情书的邮戳
    2021-01-12 10:51

    Doing network IO in the main thread is evil. Better to avoid.

    Also - your url resource access is wrong.

    Use something like this instead:

     private Bitmap bmp;
    
       new AsyncTask() {                  
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    InputStream in = new URL(IMAGE_URL).openStream();
                    bmp = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                   // log error
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result) {
                if (bmp != null)
                    imageView.setImageBitmap(bmp);
            }
    
       }.execute();
    

    This is the 'old way' of loading url resources into display. Frankly speaking, I have not written such code in a long time. Volley and Picasso simply do it much better than me, including transparent local cache, multiple loader-threads management and enabling effective resize-before-load policies. All but coffee :)

提交回复
热议问题