Hi,

I'm now switching Java. Given a URL for an image, my application wants
to read the contents of the image and get to image height & width via
Image API.

Here's my code. But the format is not correct parsed by Image Service.

String strUrl = "http://lh5.ggpht.com/_7zgrL7SM-9s/SVeaMu07FKI/
AAAAAAAAAaE/r1fP9ufeCpg/s800/add_to_chart.jpg";

InputStream inputStream = HttpRequest.getResponseStream(strUrl);
byte[] content = HttpRequest.getBytes(inputStream);
Image image = ImagesServiceFactory.makeImage(content);


public static InputStream getResponseStream(String url)
{
        try
        {
                HttpURLConnection connection = (HttpURLConnection)new URL
(url).openConnection();
                connection.setRequestMethod("GET");
                connection.setDoInput(true);

                connection.connect();
                return connection.getInputStream();
        }
        catch (IOException e)
        {
                e.printStackTrace();
        }
        return null;
}


public static byte[] getBytes(InputStream inputStream) throws
IOException
 {
        int bufferLength = 1024;
        byte[] buffer = new byte[bufferLength];
        ArrayList<byte[]> list = new ArrayList<byte[]>();

        int readLength, totalLength = 0;
        while ((readLength = inputStream.read(buffer, 0, bufferLength)) >= 0)
        {
                if (readLength == bufferLength)
                {
                        list.add(buffer);
                }
                else
                {
                        byte[] temp = new byte[readLength];
                        System.arraycopy(buffer, 0, temp, 0, readLength);
                        list.add(temp);
                }

                totalLength += readLength;
        }

        byte[] returnBytes = new byte[totalLength];
        for (int i = 0, pos = 0; i < list.size(); i ++)
        {
                byte[] currentArray = list.get(i);
                System.arraycopy(currentArray, 0, returnBytes, pos,
currentArray.length);
                pos += currentArray.length;
        }
        return returnBytes;
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to