Issue regarding the uploading of image files in to server from android
<p>I have created an application, using which I can upload an image stored at the gallery of my phone to the server. The server side language that I am using is PHP. The issue that I am facing is little bit strange. I am able to upload photos of size less that 2 MB, which I have taken using the camera of my phone. But certain photos are not getting uploaded. Here are the details of photos which were not getting uploaded:</p>

<p><strong>First photo:</strong></p>

<p>Title: 2013-03-15 09.17.22.jpg</p>

<p>Type: JPEG</p>

<p>Size: 2645Kb</p>

<p><strong>Second photo:</strong></p>

<p>Title: 2013-03-12 11.48.04.jpg</p>

<p>Type: JPEG</p>

<p>Size: 2781Kb</p>

<p>After the unsuccessful uploading of above photos I tried to upload another photo and it worked. Here are the details of the photo that I was able to upload:</p>

<p>Title: 2013-03-14 10.20.16.jpg</p>

<p>Type: JPEG</p>

<p>Size: 1238Kb</p>

<p>Then tried with few other photos less that 2000 kb and all those photos got uploaded.</p>

<p>Then I download a jpg image of 6 MB from net and tried to upload it. Surprisingly it got uploaded. Now I am totally confused, what the real issue is. Finally I have reached a conclusion that, the problem is only for those photos which are taken using the camera of my phone and having a size more than 2000 kb.</p>

<p><strong>Here is the android function that I am using to upload photos in to the server:</strong></p>

<pre><code>public String doFileUpload(String selectedPath,String page)
        {
            String response=null;
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1*1024*1024;
            String urlString = Connector.URL+page;
            try
            {
                //------------------ CLIENT REQUEST
                FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
                // open a URL connection to the Servlet
                URL url = new URL(urlString);
                // Open a HTTP connection to the URL
                conn = (HttpURLConnection) url.openConnection();
                // Allow Inputs
                conn.setDoInput(true);
                // Allow Outputs
                conn.setDoOutput(true);
                // Don't use a cached copy.
                conn.setUseCaches(false);
                // Use a post method.
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
                dos = new DataOutputStream( conn.getOutputStream() );
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data;name=uploadedfile;filename=" + selectedPath + "" + lineEnd);


                dos.writeBytes(lineEnd);
                // create a buffer of maximum size
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                // read file and write it into form...
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                while (bytesRead &gt; 0)
                {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }
                // send multipart form data necesssary after file data...
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                // close streams
                fileInputStream.close();
                dos.flush();
                dos.close();
            }
            catch (MalformedURLException ex)
            {
                Log.e("Debug", "error: " + ex.getMessage(), ex);
            }
            catch (IOException ioe)
            {
                Log.e("Debug", "error: " + ioe.getMessage(), ioe);
            }
            //------------------ read the SERVER RESPONSE
            try 
            {
              inStream = new DataInputStream ( conn.getInputStream() );
              response = inStream.readLine();
              inStream.close();

            }
            catch (IOException ioex){
                Log.e("Debug", "error: " + ioex.getMessage(), ioex);
            }
            return response;
        }
</code></pre>

<p>Here is the PHP code for the page that I use to upload images:</p>

<pre><code>&lt;?php
$tt=$_GET["id"];
// Where the file is going to be placed
$target_path = "uploads/";

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
{   
    include("../../classes/Registration.php");
    $Obj=new register();
    $Obj-&gt;insert_photo_data($tt,basename( $_FILES['uploadedfile']['name']));
}
else
{
    echo '[{"flag":"0"}]';
}
?&gt;
</code></pre>

<p>What I am getting back when the image uploading problem occurs is:</p>

<pre><code>[{"flag":"0"}]
</code></pre>
