【问题标题】:Android SDK Obtain image from HTTP getAndroid SDK 从 HTTP get 获取图像
【发布时间】:2012-10-22 13:41:40
【问题描述】:

我在下载和放入从 ULR 下载的 ImageView 图像时遇到问题,通常使用起来很容易

imgView.setImageDrawable(grabImageFromUrl(url));

但在我的情况下,我需要使用我编写的 HttpClient 来检查证书。我想我必须使用 HTTPGEt,在响应 Json 字符串之前我已经使用过它,但我就是找不到抓取图片的方法。

有人有这种操作的经验吗?

【问题讨论】:

    标签: java android http imageview http-get


    【解决方案1】:

    其实我已经找到了解决问题的方法,如果有用户遇到类似问题,我会在这里发布:

    Bitmap bmp =null;
            DefaultHttpClient client = new MyHttpClient(ShowNotification.this);
            HttpGet get = new HttpGet(url);
            HttpResponse getResponse = null;
            try {
                getResponse = client.execute(get);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String entityContents="";
            HttpEntity responseEntity = getResponse.getEntity();
            BufferedHttpEntity httpEntity = null;
            try {
                httpEntity = new BufferedHttpEntity(responseEntity);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            InputStream imageStream = null;
            try {
                imageStream = httpEntity.getContent();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            bmp = BitmapFactory.decodeStream(imageStream);
    
            ImageView imgView =(ImageView)findViewById(R.id.imgView);
            imgView.setImageBitmap(bmp);
    

    【讨论】:

      【解决方案2】:

      我向你推荐这个优秀的小型图书馆项目:

      Android-Universal-Image-Loader

      它允许异步加载。

      【讨论】:

        【解决方案3】:

        您只需将图像 URL 传递给您的方法:grabImageFromUrl(url)。可能有什么问题。你能告诉我们它的内容吗?

        这个简单的教程可以帮助您解决问题 http://www.vogella.com/articles/AndroidNetworking/article.html

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-13
        • 1970-01-01
        • 2018-12-05
        • 1970-01-01
        • 2021-03-29
        相关资源
        最近更新 更多