Load image from url

前端 未结 16 2248
独厮守ぢ
独厮守ぢ 2020-11-22 05:12

I have an image URL. I want to display an image from this URL in an ImageView but I am unable to do that.

How can this be achieved?

16条回答
  •  爱一瞬间的悲伤
    2020-11-22 05:36

    public class MainActivity extends Activity {
    
        Bitmap b;
        ImageView img;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            img = (ImageView)findViewById(R.id.imageView1);
            information info = new information();
            info.execute("");
        }
    
        public class information extends AsyncTask
        {
            @Override
            protected String doInBackground(String... arg0) {
    
                try
                {
                    URL url = new URL("http://10.119.120.10:80/img.jpg");
                    InputStream is = new BufferedInputStream(url.openStream());
                    b = BitmapFactory.decodeStream(is);
    
                } catch(Exception e){}
                return null;
            }
    
            @Override
            protected void onPostExecute(String result) {
                img.setImageBitmap(b);
            }
        }
    }
    

提交回复
热议问题