• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Image gets corrupted sending over socket

RazzleFnDazzle

Well-Known Member
I am sending an image from my android device to my computer using a socket. The servers are communicating fine but when the file reaches the computer it cannot be opened to view. Is there is some sort of reason why it won't be a direct byte copy from my phone to my computer? I've spent hours on this modifying a bit by bit and I can't get anything to work. It seems pretty straight forward, but I must be doing something wrong.

Code Snippets:

Client:
Code:
FileInputStream is;
Socket socket;
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
//send an object with data first here
objectOutputStream.flush();

byte[] b = new byte[socket.getSendBufferSize()];
int read = 0;
while ((read = is.read(b)) != -1) {
   objectOutputStream.write(b, 0, read);
   objectOutputStream.flush();
   b = new byte[socket.getSendBufferSize()];
}

objectOutputStream.close();
is.close();
socket.close();


Server:
Code:
Socket socket;
ObjectInputStream reader;
FileOutputStream fos;

//received object with data handled it

byte[] b = new byte[socket;.getReceiveBufferSize()];
int read = 0;
while ((read = reader.read(b)) != -1) {
   fos.write(b, 0, read);
   fos.flush();
   b = new byte[socket;.getReceiveBufferSize()];
}

fos.close();
 
I take it back. This code works. I don't know what happened. I think it might be that I am storing it on a fat32 drive now. Who knows. I'm just going to be happy with the fact it works.
 
Back
Top Bottom