Image not showing on image view

后端 未结 3 1459
猫巷女王i
猫巷女王i 2021-01-27 22:55

I am trying to set image on imageview but image is not show.

I am reading image url from json data and then trying to set it on ImageView but my image is not visi

相关标签:
3条回答
  • 2021-01-27 23:34

    Please Use below code for get image from url and display into imageview.

    public class image extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
    
            RelativeLayout mRlayout1 = (RelativeLayout) findViewById(R.id.mRlayout1);
            Drawable d=new BitmapDrawable(bitmap);
            mRlayoutLogin.setBackgroundDrawable(d);
        }
    
        private InputStream OpenHttpConnection(String urlString) throws IOException {
            InputStream in = null;
            int response = -1;
    
            URL url = new URL(urlString);
            URLConnection conn = url.openConnection();
    
            if (!(conn instanceof HttpURLConnection))
                throw new IOException("Not an HTTP connection");
    
            try {
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect();
                response = httpConn.getResponseCode();
                if (response == HttpURLConnection.HTTP_OK) {
                    in = httpConn.getInputStream();
                }
            } catch (Exception ex) {
                throw new IOException("Error connecting");
            }
            return in;
        }
    
        private Bitmap DownloadImage(String URL) {
            Bitmap bitmap = null;
            InputStream in = null;
            try {
                in = OpenHttpConnection(URL);
                bitmap = BitmapFactory.decodeStream(in);
                in.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            return bitmap;
        }
    }
    
    0 讨论(0)
  • 2021-01-27 23:36

    you can view image by using this code.

        try {
        bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2021-01-27 23:54

    It seems you are downloading the image from UI thread. this will block the UI thread and will give you not responding error. as an easy way, you can use a library like Universal Image Loader

    Universal Image Loader - GitHub

    this will manage the image loading for you and avoid problems like incorrect urls, Out Of Memory error.

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