Reading in bytes produced by PHP script in Java to create a bitmap

Posted by Kareem on Stack Overflow See other posts from Stack Overflow or by Kareem
Published on 2010-03-11T04:12:01Z Indexed on 2010/05/28 1:01 UTC
Read the original article Hit count: 292

Filed under:
|
|
|
|

I'm having trouble getting the compressed jpeg image (stored as a blob in my database).

here is the snippet of code I use to output the image that I have in my database:


if($row = mysql_fetch_array($sql))
{
    $size = $row['image_size'];
    $image = $row['image'];
    if($image == null){
        echo "no image!";
    } else {
       header('Content-Type: content/data');
       header("Content-length: $size");
       echo $image;
    }
}

here is the code that I use to read in from the server:


        URL sizeUrl = new URL(MYURL);
        URLConnection sizeConn = sizeUrl.openConnection();

        // Get The Response
        BufferedReader sizeRd = new BufferedReader(new InputStreamReader(sizeConn.getInputStream()));
        String line = "";
        while(line.equals("")){
            line = sizeRd.readLine();
        }
        int image_size = Integer.parseInt(line);

        if(image_size == 0){
            return null;
        }

        URL imageUrl = new URL(MYIMAGEURL);
        URLConnection imageConn = imageUrl.openConnection();

        // Get The Response
        InputStream imageRd = imageConn.getInputStream();


        byte[] bytedata = new byte[image_size];
        int read = imageRd.read(bytedata, 0, image_size);

        Log.e("IMAGEDOWNLOADER", "read "+ read + " amount of bytes");
        Log.e("IMAGEDOWNLOADER", "byte data has length " + bytedata.length);

        Bitmap theImage = BitmapFactory.decodeByteArray(bytedata, 0, image_size);
        if(theImage == null){
            Log.e("IMAGEDOWNLOADER", "the bitmap is null");
        }
        return theImage;

My logging shows that everything has the right length, yet theImage is always null. I'm thinking it has to do with my content type. Or maybe the way I'm uploading?

© Stack Overflow or respective owner

Related posts about java

Related posts about php