Rohit Sangal
Rohit Sangal

Reputation: 737

Android download image from server using json

my application contains few buttons, what i want is by clicking on any of the button, it will direct me to a URL which in turn take me to a json object page which then provide me an image source to be displayed on to my android device.

For eg: Button 1 -> http://a.b.c.d/loadview.htm?buttonid=B1 -> Json object(img src- url to image file) -> get displayed on my android device.

Thanks in advance:)

Upvotes: 2

Views: 8195

Answers (3)

AndroidRaji
AndroidRaji

Reputation: 907

This is working code: To download the image from server and display it in android

public void onCreate(Bundle savedInstanceState) 

{ 

Bitmap bitmap = DownloadImage("http://www.aaa.com/images//29_13.jpeg");

ImageView img = (ImageView) findViewById(R.id.imagefromserver);

img.setImageBitmap(bitmap);

}


private InputStream OpenHttpConnection(String urlString) 
    throws IOException

 {

 InputStream in = null;

        int response = -1;

        URL url = new URL(urlString); 
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))                     
            throw new IOException("Not an HTTP connection");

        try{
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect(); 

            response = httpConn.getResponseCode();                 
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();                                 
            }                     
        }
        catch (Exception ex)
        {
            throw new IOException("Error connecting");            
        }
        return in;     
    }
    private Bitmap DownloadImage(String URL)
    {        
        Bitmap bitmap = null;
        InputStream in = null;        
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;                
    }

Upvotes: 3

SAMD
SAMD

Reputation: 431

You need to open a HttpConnection with the url to image source and download it.
Here is the code: 

Bitmap getTheBitmap(String yourImgSrcUrl)
{
URL url = null;
Bitmap bitmap = null;
DataInputStream fileInputStream = null;
try
{
String urlPath = yourImgSrcUrl;
urlPath = urlPath.replace(" ", "%20"); // to replace any blank spaces
url = new URL(urlPath);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod("GET");

fileInputStream = new DataInputStream(connection.getInputStream());
byte[] bitmapBytes= new byte[fileInputStream.available()];
fileInputStream.read(bitmapBytes);
fileInputStream.close();
bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
return bitmap;
}
catch (IOException e) 
        {
            e.printStackTrace();
return null;
        }
}


Kindly check that Bitmap you are getting is not null before setting in the ImageView else it will throw exception.

Upvotes: 1

Rob Angelier
Rob Angelier

Reputation: 2333

Your example url does not work, but i'll try to explain anyway:

    WebRequest request = HttpWebRequest.Create("test.aspx");
    WebResponse response = request.GetResponse();

    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
    {
        string json = streamReader.ReadToEnd();

        JavaScriptSerializer ser = new JavaScriptSerializer();
        var deserializedObject = ser.Deserialize(json, typeof(ControllerBuilder));

        if(deserializedObject != null)
            //cast object to the correct type and use it.
    }

I don't recommend using the Build-in JavaScriptSerializer class, just because i had performance issues with it. A better tool to use would be:

http://james.newtonking.com/pages/json-net.aspx

Hope this helps you out!

Upvotes: 0

Related Questions