1

I am uploading some images from android to server using php. The image file is getting uploaded but when I try to view that file its showing broken image don't know what exactly is the issue. The upload is done in the AsyncTask Android Code

    FileInputStream fileInputStream;
    HttpURLConnection connection;
    URL url;
    DataOutputStream outputStream;
    int bytesRead,bytesAvailable,bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File selectedFile = new File(selectedImagePath);
try
        {
            fileInputStream = new FileInputStream(selectedFile);
            url = new URL("The url of my server");
            connection= (HttpURLConnection)url.openConnection();
            Log.v(TAG,"connection established");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection","Keep-Alive");
            connection.setRequestProperty("ENCTYPE","multipart/form-data");
            connection.setRequestProperty("Content-Type","multipart/form-data;boundary=*****");
            connection.setRequestProperty("uploaded_file",selectedImagePath);

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

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

            outputStream.writeBytes("Content-Disposition:form-data;name=\"uploaded_file\";filename=\""+selectedImagePath+"\""+"\r\n");
            outputStream.writeBytes("\r\n");
            outputStream.writeBytes("--*****\r\n");
            bytesAvailable = fileInputStream.available();
            bufferSize =  Math.min(bytesAvailable,maxBufferSize);
            buffer = new byte[bufferSize];

            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.writeBytes("--*****\r\n");
            int  serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            if(serverResponseCode  == HttpURLConnection.HTTP_OK) {
                String line;
                StringBuilder sb = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }


            }
            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
PHP Code:
 $file_path = basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path) )
{
    echo "success";
} else
{
    echo "fail";
}

Image shown on server: enter image description here

3
  • outputStream.write(buffer,0,bufferSize);. That should be outputStream.write(buffer,0,bytesRead);. Commented Sep 22, 2016 at 7:18
  • Please compare file size of original and uploaded one. Commented Sep 22, 2016 at 7:20
  • Thanks for your reply but its done now!!The issue was with the closing of the file block . Commented Sep 22, 2016 at 7:31

1 Answer 1

0

To write byte array or base64 format for image instead of multipart would be easy solution.

Check this : Uploading image from android to php server

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your quick reply but its showing the same broken image after adding header @Jai
yeah because you are passing content-type "multipart" from the client side. Use base64 format instead of multipart. that would be easy for image uploading process
Thanks for ur reply . But i got the solution and its working now using multipart.Actually tht issue was i was ending the block of image using this 'outputStream.writeBytes("--*****\r\n");' before sending the byte array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.