Load Image in ImageView from URL stored in JSONString in Android
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) { } });
You could extract the image name and url like this
public static final String JSON_STRING="{\"WebImages\":{\"Imagename\":\"image_name\",\"imageurl\":http://www.example.com/image/example.png}}"; JSONObject jsonObject = new JSONObject(JSON_STRING);JSONObject webImages = jsonObject.getJSONObject("WebImages");String imageName = webImages.getString("Imagename");String imageUrl = webImages.getString("imageurl");
Now, you have the imageName and the imageUrl. You can easily set the text doing something like myTextView.setText(imageName)
. For loading the image into ImageView, I suggest using the Picasso library. It is extremely easy to use. All you need is one line of code, which would look like
Picasso.with(context).load(imageUrl).into(imageView);