Calling Image From Server In Android
Hello i have this Json output and i am calling in my android app. I am able to get picture path but how can i display picture instead of path . here is the code public class Test
Solution 1:
I have used this class for downloading image from server Hope it will help you...!! When you have got your image URLs list from your server or any source, then used it like this to download that particular Image.
GetImage.downloadFile("pictures/file83915.jpg", newImageDownloaded()
{
@OverridepublicvoidimageDownloaded(Bitmap result){
image.setImageBitmap(result);
}
@OverridepublicvoidimageDownloadedFailed(){
}
});
Where the GetImage class is:
publicclassGetImage
{
publicstaticvoiddownloadFile(final String fileUrl, final ImageDownloaded img)
{
AsyncTask<String , Void, Bitmap> task = newAsyncTask<String , Void, Bitmap>()
{
@OverrideprotectedBitmapdoInBackground(String... params) {
Bitmap bmImg;
URL myFileUrl =null;
if(fileUrl.equals(""))
{
returnnull;
}
try
{
myFileUrl= newURL("http://yourURl/" +fileUrl.replace(" ", "%20"));
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
return bmImg;
}
catch (IOException e)
{
e.printStackTrace();
}
returnnull;
}
@OverrideprotectedvoidonPostExecute(Bitmap results)
{
if(results != null)
img.imageDownloaded(results);
else
img.imageDownloadedFailed();
}
};
task.execute("");
}
publicstaticabstractclassImageDownloaded
{
publicabstractvoidimageDownloaded(Bitmap result);
publicabstractvoidimageDownloadedFailed();
}
}
I used CustomAdapter
class to show the data in the list with Images like this.
in the method getView()
I used this thread like this.
public View getView(params....)
{
View row ;//Inflataion blah blahBitmapthumb= myCustomObject.getBitmap();
finalImageViewimage= (ImageView) row.findViewById(R.id.image);
if(thumb == null)
{
GetImage.downloadFile(myCustomObject.getImagePath(), newImageDownloaded()
{
@OverridepublicvoidimageDownloaded(Bitmap result){
myCustomObject.setBmp(result);
image.setImageBitmap(myCustomObject.getBitmap());
}
@OverridepublicvoidimageDownloadedFailed(){
}
});
}
else
image.setImageBitmap(thumb);
}
MyCustomObject
is my class
that encapsulates the data from server as well as Image from server and implements Parcelable interface
. First I get data through JSON
and then get Image in Adapter
. It can also be passed to the any DetailActivity
Post a Comment for "Calling Image From Server In Android"