text: Q:
  
  How to solve OutOfMemory Exception when uploading image?
  
  I am able to upload to the server the image of file size less than probably 1Mb and will get the OutOfMemory error if i upload more than 1Mb. I have used the BitmapFactory to decode the file but still unable to upload large image files. 
  12-15 22:33:51.465: E/AndroidRuntime(11968): java.lang.OutOfMemoryError
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:451)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at sp.com.NewProductActivity.onActivityResult(NewProductActivity.java:137)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.app.Activity.dispatchActivityResult(Activity.java:5390)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.app.ActivityThread.access$1200(ActivityThread.java:140)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.os.Handler.dispatchMessage(Handler.java:99)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.os.Looper.loop(Looper.java:137)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at android.app.ActivityThread.main(ActivityThread.java:4921)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at java.lang.reflect.Method.invokeNative(Native Method)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at java.lang.reflect.Method.invoke(Method.java:511)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
  12-15 22:33:51.465: E/AndroidRuntime(11968):    at dalvik.system.NativeStart.main(Native Method)
  
  NewProductActivity.java
  public int uploadFile(final String sourceFileUri) {
  
      String fileName = sourceFileUri;
  
      HttpURLConnection conn = null;
      DataOutputStream dos = null;
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024 * 1024;
      File sourceFile = new File(sourceFileUri);
  
      if (!sourceFile.isFile()) {
  
          dialog.dismiss();
  
          Log.e("uploadFile", "Source File not exist :" + imagepath);
  
          runOnUiThread(new Runnable() {
              public void run() {
                  messageText.setText("Source File not exist :" + imagepath);
              }
          });
  
          return 0;
  
      } else {
          try {
  
              // Edit Text
              inputName = (EditText) findViewById(R.id.inputName);
              inputLocation = (EditText) findViewById(R.id.inputLocation);
              inputDesc = (EditText) findViewById(R.id.inputDesc);
  
              //Radio Button
              reportTypeGroup = (RadioGroup) findViewById(R.id.typeOfReport);
              reportType = "";
  
              switch (reportTypeGroup.getCheckedRadioButtonId()) {
  
              case R.id.hazard:
                  reportType = "Hazard";
                  break;
  
              case R.id.incident:
                  reportType = "Incident";
                  break;
  
              default:
                  reportType = "Unknown";
                  break;
              }
  
              FileInputStream fileInputStream = new FileInputStream(sourceFile);
              URL url = new URL(upLoadServerUri);
              conn = (HttpURLConnection) url.openConnection();
              conn.setDoInput(true); // Allow Inputs
              conn.setDoOutput(true); // Allow Outputs
              conn.setUseCaches(false); // Don't use a Cached Copy
             // conn.setChunkedStreamingMode(1024);
              conn.setRequestMethod("POST");
              conn.setRequestProperty("Connection", "Keep-Alive");
          //  conn.setRequestProperty("ENCTYPE", "multipart/form-data");
              conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
              conn.setRequestProperty("uploaded_file", fileName);
  
              dos = new DataOutputStream(conn.getOutputStream());
  
              dos.writeBytes(twoHyphens + boundary + lineEnd);
  
  //Adding Parameters
  
              dos = new DataOutputStream(conn.getOutputStream());
              dos.writeBytes(twoHyphens + boundary + lineEnd);
              dos.writeBytes("Content-Disposition: form-data; name=\"name\""
                      + lineEnd);
              dos.writeBytes(lineEnd);
              dos.writeBytes(inputName.getText().toString());
              dos.writeBytes(lineEnd);
  
              dos.writeBytes(twoHyphens + boundary + lineEnd);
              dos.writeBytes("Content-Disposition: form-data; name=location"
                      + lineEnd);
              dos.writeBytes(lineEnd);
              dos.writeBytes(inputLocation.getText().toString());
              dos.writeBytes(lineEnd);
  
              dos.writeBytes(twoHyphens + boundary + lineEnd);
              dos.writeBytes("Content-Disposition: form-data; name=description"
                      + lineEnd);
              dos.writeBytes(lineEnd);
              dos.writeBytes(inputDesc.getText().toString());
              dos.writeBytes(lineEnd);
  
              dos.writeBytes(twoHyphens + boundary + lineEnd);
              dos.writeBytes("Content-Disposition: form-data; name=type"
                      + lineEnd);
              dos.writeBytes(lineEnd);
              dos.writeBytes(reportType.toString());
              dos.writeBytes(lineEnd);
  
  //Adding Parameter media file(audio,video and image)
  
              dos.writeBytes(twoHyphens + boundary + lineEnd);
  
              dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + 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 > 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);
  
              serverResponseCode = conn.getResponseCode();
              String serverResponseMessage = conn.getResponseMessage();
  
              Log.i("uploadFile", "HTTP Response is : "+ serverResponseMessage + ": " + serverResponseCode);
  
              if (serverResponseCode == 200) {
  
                  runOnUiThread(new Runnable() {
                      public void run() {
                          String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
                                  + "c:/wamp/www/echo/uploads";
                          messageText.setText(msg);
                          Toast.makeText(NewProductActivity.this,
                                  "File Upload Complete.", Toast.LENGTH_SHORT)
                                  .show();
                      }
                  });
              }
  
              // close the streams //
              fileInputStream.close();
              dos.flush();
              dos.close();
  
          } catch (MalformedURLException ex) {
  
              dialog.dismiss();
              ex.printStackTrace();
  
              runOnUiThread(new Runnable() {
                  public void run() {
                      messageText
                              .setText("MalformedURLException Exception : check script url.");
                      Toast.makeText(NewProductActivity.this,
                              "MalformedURLException", Toast.LENGTH_SHORT)
                              .show();
                  }
              });
  
              Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
          } catch (final Exception e) {
  
              dialog.dismiss();
              e.printStackTrace();
  
              runOnUiThread(new Runnable() {
                  public void run() {
                      messageText.setText("Got Exception : "+e.toString());
                      Toast.makeText(NewProductActivity.this,
                              "Got Exception : see logcat ",
                              Toast.LENGTH_SHORT).show();
                  }
              });
              Log.e("Upload file to server Exception",
                      "Exception : " + e.getMessage(), e);
          }
          dialog.dismiss();
          return serverResponseCode;
      }
  }
  }
  
  -------EDIT--------
  After adding the below code, I do not get anymore OutOfMemory exception and it says "upload successful", but nothing was uploaded.
   final BitmapFactory.Options options = new BitmapFactory.Options();
  options.inSampleSize = 8;
  
  Bitmap bm = BitmapFactory.decodeFile(strPath,options);
  imageView.setImageBitmap(bm);
  
  A:
  
  1. for out of memory error add below line to your Manifest file inside <application> tag-
  `<application
        ...
        android:largeHeap="true"
   </application>`
  
  2. Your way of programming is totally wrong for layout initialization like
   inputName = (EditText) findViewById(R.id.inputName);
   inputLocation = (EditText) findViewById(R.id.inputLocation);
   inputDesc = (EditText) findViewById(R.id.inputDesc);
  
  this should be in onCreate(Bundle arg0) for best practice 
  and one more for image uploading like process you have to use AsyncTask for ex : How can I use an Async Task to upload File and Resize your bitmap file to avoid such errors
  3.for large image problem you have to change setting of php server.
  by default http request allow to upload data upto 2 mb on a php server.if you want to increase upload limit then go to your php config file and set upload limit to what ever you want.
  
  