1

I'm trying to send an jpeg image from my android phone through socket and from the PC part, get the sent data and store it in an jpg file.

I'm pretty sure that I configured the socket correctly, as I can download data (binary file) from PC to android and save it correctly.

I can also read the stream which is sent from android to PC. The packet length and header information are exactly what I expect.

The problem is in reading image data. I'm getting same size for image data but when I save it to .jpg file, it is corrupted and I can not view it.

Here is my Android code that tries to send image file after sending header information:

try{
        //packagesize is header information which is sent in advance
        index.putInt(packagesize);

        byte c[]= {index.get(3),index.get(2),index.get(1),index.get(0)};

        InputStream jpgimage = new FileInputStream(fileimage);          

        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        dataInputStream = new DataInputStream(socket.getInputStream());

        int writeBytes = 0,len = 0;
        byte buffer[] = new byte[1024];

        while((len = jpgimage.read(buffer,0,buffer.length))!=-1)
        {
            writeBytes+=len;                
            dataOutputStream.write(buffer,0,len);

        }       

        dataOutputStream.flush();
        jpgimage.close();
        dataInputStream.close();
        dataOutputStream.close();

         ...
         ...
         ...
 }
 catch statements here

This is the receiving code in the PC part:

    // after reading header information I try to read image data

    char *buff = malloc(sizeof(char) * jpeg_length); 

unsigned int byteCount = 0;
unsigned int byteCount = 0;
do
{
    int ret = recv(socket, buff+readBytes, jpeg_length-readBytes, 0);
    if (ret <= 0)
    {
        fprintf(stderr,"Error receiving jpeg file.\n");
        fclose( output );
        return 106;
    }
            readBytes += ret;

    fwrite(buff, sizeof(char), readBytes, output);
}
while (readBytes < jpeg_length);

fclose( output );

I also have to mention that the receiving part is working fine when I send image data with PC client application which is pure C++.

Is there any idea about what is the problem and why I get corrupted image sending from android device?

Appreciate it.

Edited I add this the to android application for testing if the sending bytes can form a good image or not? I saved the image and it was OK.

int writeBytes = 0,len = 0;
byte buffer[] = new byte[1024];
// Here I save all sending bytes to an image called test.jpg
String path = "sdcard/download/images/test.jpg"; 
FileOutputStream stream = new FileOutputStream(path); 

        while((len = jpgimage.read(buffer,0,buffer.length))!=-1)
        {
            writeBytes+=len;
            stream.write(buffer); 
            dataOutputStream.write(buffer,0,len);
            dataOutputStream.flush();

        }
        stream.flush();

        dataOutputStream.flush();
        jpgimage.close();
        dataInputStream.close();
        dataOutputStream.close();
Micha
  • 5,117
  • 8
  • 34
  • 47
Hamid Bazargani
  • 847
  • 1
  • 15
  • 30

2 Answers2

1

I think you should use Bitmap class to convert you image to ByteBuffer and then send it across and on the other end convert ByteBuffer to image.

On Sender Side

Bitmap bitmap = BitmapFactory.decodeFile("ImageD2.jpg");
int bytes = bitmap.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bitmap.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();

Now you can send byte[] as normal data.

On receiving side

receive the array normally and convert it back to Bitmap

 Bitmap bitmap = BitmapFactory.decodeByteArray(array , 0, array .length);

for more information you can read following questions

Converting bitmap to byteArray android

How to convert byte array to Bitmap

Community
  • 1
  • 1
kaysush
  • 4,797
  • 3
  • 27
  • 47
  • My receiving side is not java. so how can I decode byteArray to bitmap and save it to jpeg file in c++ PC part? By the way, Would that really solve the problem. cause I can save the bytes to image properly in android part(see edited post) – Hamid Bazargani Jul 04 '13 at 05:19
  • @HamidBazargani try writing those bytes directly to a file. – kaysush Jul 04 '13 at 05:23
  • Did not work. it goes to infinite loop while reading in receiving side. – Hamid Bazargani Jul 04 '13 at 05:50
  • Can you show the code where you send the file size? Have you consider the byte order? Just in case. I've made a lot of image transfer PC-Android on both sides and it Works without probelms. – ja_mesa Jul 04 '13 at 14:47
  • @ja_mesa I send file size with 4 bytes as a part of header data before sending an image. I get it in PC correctly. Can you send me the code for PC-Android transfer please – Hamid Bazargani Jul 04 '13 at 20:07
1

I found the solution for that. The problem was from Android side. So I did the following changes:

I changed DataOutputStream and DataInputStream to BufferedOutputStream and BufferedInputStream respectively :

try{
    //packagesize is header information which is sent in advance
    index.putInt(packagesize);

    byte c[]= {index.get(3),index.get(2),index.get(1),index.get(0)};

    InputStream jpgimage = new FileInputStream(fileimage);          

    dataOutputStream = new BufferedOutputStream(socket.getOutputStream());
    dataInputStream = new BufferedInputStream(socket.getInputStream());

    int writeBytes = 0,len = 0;
    byte buffer[] = new byte[1024];

    while((len = jpgimage.read(buffer,0,buffer.length))!=-1)
    {
        writeBytes+=len;                
        dataOutputStream.write(buffer,0,len);

    }       

    dataOutputStream.flush();
    jpgimage.close();
    dataInputStream.close();
    dataOutputStream.close();

     ...
     ...
     ...

} catch statements here

Hamid Bazargani
  • 847
  • 1
  • 15
  • 30