private String postPointOfInterestToServer() { try { // Make sure we have an image selected if (this.mImageUrl == null) { return "FAIL-IMAGE"; } // Make sure we have a location if (mCurrentLocation == null) { return "FAIL-LOCATION"; } Cursor cursor = getContentResolver().query(this.mImageUrl, null,null, null, null); cursor.moveToFirst(); int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); String absoluteFilePath = cursor.getString(index); FileInputStream fis = new FileInputStream(absoluteFilePath); int bytesRead = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; while ((bytesRead = fis.read(b)) != -1) { bos.write(b, 0, bytesRead); } byte[] bytes = bos.toByteArray(); // Post our byte array to the server URL url = new URL(_blobImagePostString.replace("\"", "")); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoOutput(true); urlConnection.setRequestMethod("PUT"); urlConnection.addRequestProperty("Content-Type", "image/jpeg"); urlConnection.setRequestProperty("Content-Length", ""+ bytes.length); // Write image data to server DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream()); wr.write(bytes); wr.flush(); wr.close(); int response = urlConnection.getResponseCode(); if (response == 201 && urlConnection.getResponseMessage().equals("Created")) { // We've posted succesfully, let's build the JSON data JSONObject jsonUrl = new JSONObject(); try { jsonUrl.put("Description", absoluteFilePath.substring(absoluteFilePath.lastIndexOf("/") + 1)); UUID uuid = UUID.randomUUID(); jsonUrl.put("Id", uuid.toString()); jsonUrl.put("Latitude", mCurrentLocation.getLatitude()); jsonUrl.put("Longitude", mCurrentLocation.getLongitude()); jsonUrl.put("Type", 1); jsonUrl.put("Url",this._blobImagePostString.split("\\?")[0]); } catch (JSONException e) { Log.e("AddPOI", "Exception building JSON: " + e.getMessage()); e.printStackTrace(); } HttpURLConnection newPOIUrlConnection = null; URL newPOIUrl = new URL(Constants.kAddPOIUrl); newPOIUrlConnection = (HttpURLConnection) newPOIUrl.openConnection(); newPOIUrlConnection.setDoOutput(true); newPOIUrlConnection.setRequestMethod("POST"); newPOIUrlConnection.addRequestProperty("Content-Type","application/json"); newPOIUrlConnection.setRequestProperty("Content-Length",""+ Integer.toString(jsonUrl.toString().getBytes().length)); // Write json data to server DataOutputStream newPoiWR = new DataOutputStream(newPOIUrlConnection.getOutputStream()); newPoiWR.writeBytes(jsonUrl.toString()); newPoiWR.flush(); newPoiWR.close(); int newPoiResponse = urlConnection.getResponseCode(); return newPOIUrlConnection.getResponseMessage(); } // End of post of byte array to server } catch (Exception ex) { Log.e("AddPOI", "Error in image upload: " + ex.getMessage()); } return "BIGFAIL"; }