从Android中存储在JSONString中的URL在ImageView中加载图像 [英] Load Image in ImageView from URL stored in JSONString in Android

查看:407
本文介绍了从Android中存储在JSONString中的URL在ImageView中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串,说一个名称和一个Url.我需要将名称提取到TextView中,并提取并在ImageView中显示图像. 下面是上述情况的代码.

I have a JSON string, say a name and a Url . I need to extract name into the TextView and extract and display image in ImageView. Below is the code for above scenario.

 public static final String JSON_STRING="{\"WebImages\":{\"Imagename\":\"image_name\",\"imageurl\":http://www.example.com/image/example.png}}"; 

我需要在创建的TextView中显示名称,然后从该url获取图像并显示在Imageview中.

I need to display the name in TextView which I created, and fetch the image from that url and display in the Imageview.

推荐答案

首先使用Gson解析Json并以字符串形式获取url,然后可以使用UniversalImageLoader库(它可以进行异步图像下载,并且非常易于使用) https://github.com/nostra13/Android-Universal-Image-Loader

First of all use Gson to parse Json and get url as a String, then you can use UniversalImageLoader library (it does the async image download and very easy to use) https://github.com/nostra13/Android-Universal-Image-Loader

static public DisplayImageOptions options= new DisplayImageOptions.Builder().cacheOnDisk(true).cacheInMemory(true).build();
    imageLoader.displayImage(imgUrl, ivImage, options, new ImageLoadingListener() {

        public void onLoadingStarted(String imageUri, View view) {
            ((ImageView)view).setImageResource(R.drawable.nofoto);
        }

        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            ((ImageView)view).setImageResource(R.drawable.nofoto);
        }

        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

        }

        public void onLoadingCancelled(String imageUri, View view) {
        }

    });

这篇关于从Android中存储在JSONString中的URL在ImageView中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆