Load Image in ImageView from URL stored in JSONString in Android(从存储在 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 为 String,然后可以使用 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 中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从存储在 Android 中 JSONString 中的 URL 加载 ImageView 中的图像
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
