views:

53

answers:

3

I'm developing an android application and I'm trying to upload an image to our server from the client side. When I get onto the server to view the image it is corrupted (gray bar on bottom) sometimes. I'm not sure why this is happening. If anyone can shed some light on this issue that would be greatly appreciated. Here is the code that uploads the image.

try {

            url = new URL(SERVER_URL);

            connection = (HttpURLConnection)url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "image/jpeg");
            connection.setChunkedStreamingMode(STREAM_CHUNK_SIZE_KB * 1024);
            connection.connect();

            outputStream = new DataOutputStream(connection.getOutputStream());

            // Write file header (userId; Id; contentId; MediaType; size)
            outputStream.writeLong(mUserId);
            outputStream.writeLong(mId);
            outputStream.writeLong(file.getId());
            outputStream.writeUTF(MediaType.getMediaType(file.getMediaType()));
            outputStream.writeInt(file.getSize());

            // Write file data to stream
            int maxBufferSize = (8 * 1024);
            FileInputStream fileInputStream = new FileInputStream(file.getImageFile());
            int bytesAvailable = fileInputStream.available();

            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[] buffer = new byte[bufferSize];
            int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            outputStream.writeBytes("\r\n");

            outputStream.flush();

            // Check response
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                uploadStatus = true;

                InputStream responseStream = connection.getInputStream();
                BufferedReader responseReader = new BufferedReader(new InputStreamReader(responseStream));

                char[] response = new char[4];
                responseReader.read(response, 0, 4);
                responseReader.close();

                int responseValue = 0;
                for (int b = 0; b < 4; b++) {
                    responseValue |= response[b] & 0xFF;
                    if (b < 3) {
                        responseValue <<= 8;
                    }
                }

                switch (responseValue) {
                    case SAVED_SUCCESSFULLY:
                        Log.d("FileUploader::upload -> Server response: Upload successful");
                        break;
                    case ERROR_SAVING_FILE:
                        Log.d("FileUploader::upload -> Server response: Upload failed");
                        break;
                    case FILE_MORE_THAN_ALLOWED_SIZE:
                        Log.d("FileUploader::upload -> Server response: Upload failed, exceeded allowed file size");
                        break;
                }
            } else {
                Log.d("FileUploader::upload -> responseCode = " + responseCode);
                checkErrorStream(connection.getErrorStream());
            }
        } catch(Exception e) {
            Log.e(Log.getStackTraceString(e));
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch(Exception e) {
                Log.e(Log.getStackTraceString(e));
            }

            if (connection != null) {
                connection.disconnect();
            }
        }
A: 

Please try to take image from divece via USB not via network. Would that image be OK or it would be broken too?

Fedor
A: 

I can't easily tell you why the image is corrupted without building a test app and running your code, but I can give you a hint:

Save the original image to a file. Grab the image that was transfered over the network. Open up both of them in a hex editor, and then look at what the difference is. (Which is presumably near the end of the file.)

This should tell you what the actual corruption consists of, which in turn will tell you what to look for in your code.

(For example, you could be truncating your file, or appending a newline that shouldn't be there, or any number of subtle errors.)

Trevor Johns
A: 

Hi,

You may have an error in your code:

outputStream.write(buffer, 0, bufferSize);

should be

outputStream.write(buffer, 0, bytesRead);
ognian