AsyncTask ImageView from image web using setImageBitmap

后端 未结 3 1156
深忆病人
深忆病人 2020-12-21 18:57

I have a problem to display this image in my internet. I have no idea how to make it work. I am new to android.

The problem is that part ...

imView =         


        
相关标签:
3条回答
  • 2020-12-21 19:41

    You create a bitmap in doInBackground that you never use. Return instead the bitmap and use it in onPostExecute.

    0 讨论(0)
  • 2020-12-21 19:52

    Try below code to download image from web and display in imageview.

    public class MainActivity extends Activity {
    
        ImageView mImgView1;
        static Bitmap bm;
        ProgressDialog pd;
        String imageUrl = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg";
        BitmapFactory.Options bmOptions;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mImgView1 = (ImageView) findViewById(R.id.mImgView1);
            pd = ProgressDialog.show(MainActivity.this, "Aguarde...",
                    "Carregando...");
            new ImageDownload().execute("");
        }
    
        public class ImageDownload extends AsyncTask<String, Void, String> {
    
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
                bmOptions = new BitmapFactory.Options();
                bmOptions.inSampleSize = 1;
                loadBitmap(imageUrl, bmOptions);
                return imageUrl;
            }
    
            protected void onPostExecute(String imageUrl) {
                pd.dismiss();
                if (!imageUrl.equals("")) {
                    mImgView1.setImageBitmap(bm);
                } else {
                    Toast.makeText(MainActivity.this,
                            "Não foi possível obter resultados", Toast.LENGTH_LONG)
                            .show();
                }
            }
    
        }
    
        public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
            InputStream in = null;
            try {
                in = OpenHttpConnection(URL);
                bm = BitmapFactory.decodeStream(in, null, options);
                in.close();
            } catch (IOException e1) {
            }
            return bm;
        }
    
        private static InputStream OpenHttpConnection(String strURL)
                throws IOException {
            InputStream inputStream = null;
            URL url = new URL(strURL);
            URLConnection conn = url.openConnection();
    
            try {
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setRequestMethod("GET");
                httpConn.connect();
    
                if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    inputStream = httpConn.getInputStream();
                }
            } catch (Exception ex) {
            }
            return inputStream;
        }
    }
    
    0 讨论(0)
  • 2020-12-21 19:57

    hey i faced the same problem for showing image from url, this link was very useful. also u can refer to stackoverflow discussion

    Apparently android 3 onwards network connectivity is strict hence network connection fails

    Cheers!

    0 讨论(0)
提交回复
热议问题