I use this code to upload images from my device to the server
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v("Test", "IGA");
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
selectedImageUri =(Uri)data.getData();
AddCourtActivity.mImage.setImageURI(selectedImageUri);
selectedImagePath = getPath(selectedImageUri);
imagePath=selectedImagePath;
Log.v("Test","IGA "+imagePath);
} else if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
imagePath=selectedImagePath;
Log.v("Test","IGA1 "+imagePath);
AddCourtActivity.mImage.setImageBitmap(thumbnail);
}
}
if (resultCode == RESULT_CANCELED) {
return;
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public String getPath1(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow("userImage");
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
public class FileUploader {
private String Tag = "UPLOADER";
private String urlString;
HttpURLConnection conn;
String exsistingFileName;
public String result;
public String uploadImageData(String serverImageTag) {
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
Log.e(Tag, "Inside second Method");
FileInputStream fileInputStream = new FileInputStream(new File(
exsistingFileName));
URL url = new URL(urlString);
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);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
if (serverImageTag.equalsIgnoreCase("courtImage")) {
dos.writeBytes("Content-Disposition: post-data; name=courtImage[];filename="
+ exsistingFileName + "" + lineEnd);
} else if (serverImageTag.equalsIgnoreCase("userImage")) {
dos.writeBytes("Content-Disposition: post-data; name=userImage[];filename="
+ exsistingFileName + "" + lineEnd);
}
dos.writeBytes(lineEnd);
Log.e(Tag, "Headers are written");
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1000;
byte[] buffer = new byte[bytesAvailable];
int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
while (bytesRead > 0) {
dos.write(buffer, 0, bytesAvailable);
bytesAvailable = fileInputStream.available();
bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
String serverResponseMessage = conn.getResponseMessage();
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while ((result = rd.readLine()) != null) {
Log.v("result", "result " + result);
Login.fbResponse = result;
}
Log.e(Tag, "File is written");
fileInputStream.close();
dos.flush();
dos.close();
rd.close();
} catch (MalformedURLException ex) {
Log.e(Tag, "error: " + ex.getMessage(), ex);
}
catch (IOException ioe) {
Log.e(Tag, "error: " + ioe.getMessage(), ioe);
}
return result;
}
public FileUploader(String existingFileName, String urlString) {
this.exsistingFileName = existingFileName;
this.urlString = urlString;
}
_, urlString , .
}