How do I implement this image view in an async task?

后端 未结 3 517
故里飘歌
故里飘歌 2021-01-29 05:33

I have an url passed to an activity and I am trying to show the image from the url full screen, however it throws a main network thread exception.

From what I can find

3条回答
  •  一向
    一向 (楼主)
    2021-01-29 06:19

    Put your network task inside doInBackground() method of AsyncTask as follow:

    @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
    
    try {
            //ImageView i = (ImageView)findViewById(R.id.imgView);Put this line in onPreExecute() method
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
           // i.setImageBitmap(bitmap);//and this one on postExecute() method
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    

提交回复
热议问题