Topic
  • 2 replies
  • Latest Post - ‏2012-12-25T09:24:19Z by SystemAdmin
SystemAdmin
SystemAdmin
2327 Posts

Pinned topic Displaying image retrieved through HTTP adapter on a page

‏2012-12-22T11:04:25Z |
Hi,

I am using a HTTP adapter to retrieve bluepages image. But, this image is being received as an attachment if I am giving the link in the browser. Also, when I invoke the procedure by giving returnedContentType : 'plain' in the js file ( if I give any other format, getting an error "The returned InvocationResult must be a valid JSON Object." ),
I am getting the response in the web browser as follows:

{
"errors": [
],
"info": [
],
"isSuccessful": true,
"responseHeaders": {
"Cache-Control": "public, pre-check=604800, post-check=604799, max-age=604800, no-cache=set-cookie",
"Connection": "Keep-Alive",
"Content-Disposition": "attachment; filename=02816S744.jpg",
"Content-Language": "en-US",
"Content-Type": "image\/jpeg",
"Date": "Sat, 22 Dec 2012 10:53:03 GMT",
"Expires": "Thu, 01 Dec 1994 16:00:00 GMT",
"Keep-Alive": "timeout=4, max=125",
"Proxy-Connection": "Keep-Alive",
"Server": "IBM_HTTP_Server\/6.1.0.33 Apache\/2.0.47",
"Set-Cookie": "JSESSIONID=0000_AgRNgNvNAUqATuUckW4kla:139fnftal; Path=\/",
"Transfer-Encoding": "chunked",
"Via": "HTTP\/1.1 w3.ibm.com (IBM-PROXY-WTE)"
},
"statusCode": 200,
"statusReason": "OK",
"text": "����\0\u0010JFIF\0\u0001\u0001\0\0\u0001\0\u0001\0\0��\0C\0\u0004\u0003\u0003\u0004\u0003\u0003\u0004\u0004\u0003\u0004\u0005\u0004\u0004\u0005\u0006\n\u0007\u0006\u0006\u0006\u0006\n\t\n\b\n\u000f\n\u0010\u0010\u000f\n\u000f\u000e\u0011\u0013\u0018\u0014\u0011\u0012\u0017\u0012\u000e\u000f\u0015\u001c\u0015\u0017\u0019\u0019\u001b\u001b\u001b\u0010\u0014\u001d\u001f\u001d\u001a\u001f\u0018\u001a\u001b\u001a��\0C\u0001\u0004\u0005\u0005\u0006\u0005\u0006\f\u0007\u0007\f\u001a\u0011\u000f\u0011",
"warnings": [
]
}

Please help with this issue.

Thanks in advance,

Vijay
  • AntonAleksandrov
    AntonAleksandrov
    55 Posts

    Re: Displaying image retrieved through HTTP adapter on a page

    ‏2012-12-23T13:00:14Z  
    Hello Vijay,

    The best solution to display an image in your application would be to send image url to the application and add element.
    Still, if you want to retrieve image, convert it to base64 and send to your app it is possible by implementing several lines of code in java.

    This is the class I've used for retrieving image and converting it to base64:

    package com.code;

    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.net.URL;

    import javax.imageio.ImageIO;

    import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

    public class ImageProcessor {
    • public static String getImageAsBase64(String imageUrl, String imageType) throws Exception{*
    • *
    • // Get image from the internet*
    • BufferedImage img = ImageIO.read(new URL(imageUrl));*
    • *
    • // Convert image to a byteStream*
    • ByteArrayOutputStream outputStream = new ByteArrayOutputStream();*
    • ImageIO.write(img, imageType, outputStream);*
    • *
    • // Convert image to base64*
    • String base64 = Base64.encode(outputStream.toByteArray()); *
    • *
    • return base64;*
    • }*
    }
    And this is the adapter code:

    var imageProcessor = com.code.ImageProcessor;

    function getImage(){
    • var base64 = imageProcessor.getImageAsBase64("http://phonegap.com/uploads/2011/09/Worklight_logo_website.png", "png");*
    • return {*
    • base64 : base64*
    • };*
    }
  • SystemAdmin
    SystemAdmin
    2327 Posts

    Re: Displaying image retrieved through HTTP adapter on a page

    ‏2012-12-25T09:24:19Z  
    Hello Vijay,

    The best solution to display an image in your application would be to send image url to the application and add element.
    Still, if you want to retrieve image, convert it to base64 and send to your app it is possible by implementing several lines of code in java.

    This is the class I've used for retrieving image and converting it to base64:

    package com.code;

    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.net.URL;

    import javax.imageio.ImageIO;

    import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

    public class ImageProcessor {
    • public static String getImageAsBase64(String imageUrl, String imageType) throws Exception{*
    • *
    • // Get image from the internet*
    • BufferedImage img = ImageIO.read(new URL(imageUrl));*
    • *
    • // Convert image to a byteStream*
    • ByteArrayOutputStream outputStream = new ByteArrayOutputStream();*
    • ImageIO.write(img, imageType, outputStream);*
    • *
    • // Convert image to base64*
    • String base64 = Base64.encode(outputStream.toByteArray()); *
    • *
    • return base64;*
    • }*
    }
    And this is the adapter code:

    var imageProcessor = com.code.ImageProcessor;

    function getImage(){
    • var base64 = imageProcessor.getImageAsBase64("http://phonegap.com/uploads/2011/09/Worklight_logo_website.png", "png");*
    • return {*
    • base64 : base64*
    • };*
    }
    Hi Anton,

    Thanks for the response. But, I am having another problem. The image that I am retrieving is in the form of an attachment. But the one that you have mentioned is for an image on the page(in the url). So, is there a way to show the attachment on a page?

    Thanks in advance,

    Vijay