从JSON对象插入imageView,在Internet上插入图像文件

时间:2014-04-14 18:52:36

标签: android json

我在Android的列表视图中显示JSON对象。

这是我用来填充行的代码:

public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if(v == null) {
            LayoutInflater li = LayoutInflater.from(getContext());
            v = li.inflate(R.layout.ofertas_app_custom_list, null);           
        }

        Ofertas_Application app = items.get(position);

        if(app != null) {
            ImageView icon = (ImageView)v.findViewById(R.id.imageView1);
            TextView titleText = (TextView)v.findViewById(R.id.textView1);
            TextView direccionText = (TextView)v.findViewById(R.id.textView2);
            TextView valoracionText = (TextView)v.findViewById(R.id.textView3);

            if(titleText != null) titleText.setText(app.getNombreEmpresa());//populate textView1

            if (direccionText !=null) direccionText.setText(app.getDireccionEmpresa());//populate textView2

    //what I need comes here, app.getImagenEmpresa stores the image name, and the URL to the image is http://mujercanariasigloxxi.appgestion.eu/imagenes

        }

        return v;
    }

现在我需要使用名称存储在

的图像填充imageView1
app.getImagenEmpresa()

欢迎任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:1)

执行以下操作:

ImageView img = (ImageView) findViewById(R.id.imageView1);
try {
    URL url = new URL("Your URL");
    //try this url = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"
    HttpGet httpRequest = null;

    httpRequest = new HttpGet(url.toURI());

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = (HttpResponse) httpclient
            .execute(httpRequest);

    HttpEntity entity = response.getEntity();
    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
    InputStream input = b_entity.getContent();

    Bitmap bitmap = BitmapFactory.decodeStream(input);

    img.setImageBitmap(bitmap);

} catch (Exception ex) {

}    

或者使用Picasso / Volley获取相同和一些额外功能,例如缓存。

相关问题