LaVOZs

The World’s Largest Online Community for Developers

'; java - send images via sockets without corrupting it - LavOzs.Com

Hello guys i'm trying to write an android app for send image on pc over socket. I have an android client and a Java SE server. Sometimes the images arrive corrupted, and i don't know why. I have tried to send 175 photos and 9 of this is corrupted. this is the code for the android client: PS: i use ObjectInputStream and ObjectInputstream.

            try {

                //Get image
                //the variable "uri" is the uri of image
                InputStream is =  contentResolver.openInputStream(uri);
                final Bitmap bitmap = BitmapFactory.decodeStream(is);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte array[] = baos.toByteArray();
                int length = array.length;
                Log.d("MY-DEBUG", "length: " + length);

                //Send lenght of image
                out.writeInt(length);
                out.flush();

                int remainent = length;
                int send = 0;
                int size = SIZE;

                while (remainent > 0) {
                    if (remainent < size)
                        size = remainent;

                    //System.out.println("ATTUALE: " + send + ", GRANDEZZA ARRAY: " + size);
                    out.write(array, send, size);
                    int percentage = (100 * send) / length;
                    publishProgress(percentage);
                    System.out.println("\n === FINE === \n");

                    send += size;
                    remainent -= size;
                    Log.d("MY-DEBUG", "Send " + send + "/" + length + " - remainent: " + remainent);
                }

                out.flush();

                Log.d("MY-DEBUG", "Immagine inviata");
                publishProgress(0);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

and this is code for server in Java SE

try {           
        //Read length
        int length = (int) in.readInt();

        File target = new File(percorso + "\\img-" + sequenza + ".jpg");
        outs = new FileOutputStream(target, true);
        System.out.println("Grandezza: " + length);
        int remainent = length;
        int read = 0;
        int total = 0;
        int size = SIZE;

        byte buffer[] = new byte[size];

        while ((read = in.read(buffer, 0, size)) != -1) {                           
            total += read;
            remainent = length - total;
            System.out.println("read: " + read + " - Received: " + total + "/" + length + " - remainent: " + remainent);
            int percentuale = (100 * total) / length;
            progressBar.setValue(percentuale);
            outs.write(buffer);
            outs.flush();

            Thread.sleep(1);
            if (remainent == 0) break;
        }

        progressBar.setValue(0);

        //Thread.sleep(100);

        //in.read();

        System.out.println("END");
    } catch (IOException e1) {
        System.err.println(e1.getMessage());
        e1.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        if (outs != null) {
            try {
                outs.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    outs.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
        Thread.currentThread().interrupt();
    }

SIZE variable is declared like this

private static final int SIZE = 1024;

If i set a value > of 1024 ALL photos arrive corrupted. If i use 1024 some photos arrive corrupted. Thank you very much

The issue is this line:

outs.write(buffer);

You're unconditionally writing out the entire buffer, which is always SIZE bytes long. But what if you had a "short" read? Try this, instead:

outs.write(buffer,0,read);
Related
What is the difference between a port and a socket?
Strange out of memory issue while loading an image to a Bitmap object
How to lazy load images in ListView in Android
Sending Email in Android using JavaMail API without using the default/built-in app
How to send an object from one Android Activity to another using Intents?
how to access camera.java in on cick event?
null pointer exception during android listview refresh
Android - Vpnservice DatagramChannel.open() not working