send images from pc to android

1.3k Views Asked by At

I'm trying to write an application that creates images on the PC and transmits over WiFi and displays it on an Android. I have everything working except the last part. The Android and PC are sending messages back and forth. The PC creates images, converts it to a byte array, sends it to the Android, Android receives it. The thing that doesn't work is converting the byte array back to an image. Here is my code.

The C# code on my PC uses this to create the byte array

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp );
    return ms.ToArray();
}

The Java code on my Android uses this code to convert the byte array back to an image.

try {
    //This line always returns NULL
    Bitmap bmp=BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    if (bmp != null) {
        //display image in UI
        imgViewer.setImageBitmap(bmp);
        imgViewer.invalidate();
    }
    else
    {
        Log.i(Consts.TAG, "image is null ");
    }                   
} catch (Exception e){
    Log.i(Consts.TAG, "ERROR decoding image " + e.toString());
}

BitmapFactory.decodeByteArray() always returns NULL. Am I creating the byte array correctly on the PC? Should I be recreating the image differently on the Android?

1

There are 1 best solutions below

0
Mikutus On

I figured it out. I needed to convert the image like this. Now it works.

    public static byte[] ImageToByte(Image img)
    {
        ImageConverter converter = new ImageConverter();
        return (byte[])converter.ConvertTo(img, typeof(byte[]));
    }

Thanks to zapl and FoamyGuy for your input.