How to set a Image to a Image View from a URL Android

前端 未结 4 1923
Happy的楠姐
Happy的楠姐 2021-01-20 04:20

I am trying to download and show a image in my imageview from URL that i will get dyncamically .I have tried this way

 URL url = new URL(parsedWeatherRespo         


        
4条回答
  •  一个人的身影
    2021-01-20 05:13

    As you are running Network Related work on MainThread i.e. UIThread which cause UIThread not to laid out it's Viewon Screen or Activity. That's according to ThreadPolicy all the time consuming and variable operation are need to performed on AysncTask or Thread.

    As per below LongOperation is AsyncTask which can be executed by calling execute() on it.

    new LongOperation().execute();  
    

    private class LongOperation extends AsyncTask {
    
          @Override
          protected String doInBackground(Bitmap... params) 
          {
           try 
              {
              URL url = new URL(parsedWeatherResponse.getWeatherIconUrl());
              Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
              } 
           catch (InterruptedException e) {
              // TODO Auto-generated catch block
                e.printStackTrace();
               }
           return bmp;
          }      
    
          @Override
          protected void onPostExecute(Bitmap bmp) 
          {
           super.onPostExecute(result);
           weather_image.setImageBitmap(bmp);
           }
    
          @Override
          protected void onPreExecute() {
          }
    
          @Override
          protected void onProgressUpdate(Void... values) {
          }
    }   
    

提交回复
热议问题