These are global variables that need to be defined first:
private String mString; private Uri image_uri; private String response; private HttpURLConnection conn = null; private DataOutputStream dos = null; private String lineEnd = "\r\n"; private String twoHyphens = "--"; private String boundary = "*****"; private int bytesRead, bytesAvailable, bufferSize; private byte[] buffer; private String url_for_image_upload = "your_web_api_put_here"; private int maxBufferSize = 1 * 1024 * 1024;
// then call these two methods on the onclick () method button
mString = getRealPathFromURI(image_uri); ImageUpload();
// and write these methods as follows
private void ImageUpload() { Toast.makeText(getApplicationContext(), "Please Wait while uploading Image", Toast.LENGTH_SHORT).show(); try { FileInputStream fileInputStream = new FileInputStream(new File(mString)); URL url = new URL(url_for_image_upload); conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); 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=\"img_name\";filename=\"img_name" + "\"" + lineEnd); dos.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; 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); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream())); Log.d("BuffrerReader", "" + in); if (in != null) { response = convertStreamToString(in); Log.e("FINAL_RESPONSE-LENGTH",""+response.length()); Log.e("FINAL_RESPONSE", response); } fileInputStream.close(); dos.flush(); dos.close(); if (response.startsWith("0")) { Toast.makeText(getApplicationContext(), "Image Uploaded not successfully", Toast.LENGTH_SHORT) .show(); } else { Toast.makeText(getApplicationContext(), "Image Uploaded successfully", Toast.LENGTH_SHORT) .show(); } } catch (MalformedURLException ex) { Log.e("Image upload", "error: " + ex.getMessage(), ex); } catch (IOException ioe) { Log.e("Image upload", "error: " + ioe.getMessage(), ioe); } } public String getRealPathFromURI(Uri contentUri) { String[] proj = { MediaColumns.DATA }; @SuppressWarnings("deprecation") Cursor cursor = managedQuery(contentUri, proj, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaColumns.DATA); cursor.moveToFirst(); mString = cursor.getString(column_index); return mString; } public static String convertStreamToString(BufferedReader is) throws IOException { if (is != null) { StringBuilder sb = new StringBuilder(); String line; try { while ((line = is.readLine()) != null) { sb.append(line).append(""); } } finally { is.close(); } return sb.toString(); } else { return ""; } }
Hiren Patel Apr 11 '13 at 6:41 2013-04-11 06:41
source share