I am using the following code to upload an image to a php server. Please note that the uploadFile function you just need to transfer your image path from the SD card.
public class PhotosActivity extends Activity { ImageView img, img1; int column_index; Intent intent = null; Bitmap bitmap = null; FileInputStream in1, in2, in3; BufferedInputStream buf; // Declare our Views, so we can access them later String logo, imagePath, Logo; Cursor cursor; private String Tag = "UPLOADER"; private String urlString = "YOUR_ONLINE_PHP"; HttpURLConnection conn; // YOU CAN EDIT THIS TO WHATEVER YOU WANT private static final int SELECT_PICTURE = 1; String selectedImagePath; // ADDED String filemanagerstring; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.photos1_layout); // ListView iv= (ListView)findViewById(R.id.listView1); // iv.setAdapter(new ArrayAdapter(this, // android.R.layout.simple_list_item_1, values)); // iv.setAdapter(new // ArrayAdapter(this,android.R.layout.simple_list_item_1, values)); img = (ImageView) findViewById(R.id.imageView1); } public void onClick(View v) { // select a file Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); } String path = ""; // UPDATED @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == SELECT_PICTURE) { Uri selectedImageUri = data.getData(); // OI FILE Manager filemanagerstring = selectedImageUri.getPath(); // MEDIA GALLERY selectedImagePath = getPath(selectedImageUri); img.setImageURI(selectedImageUri); imagePath.getBytes(); path = imagePath.toString(); // TextView txt = (TextView)findViewById(R.id.title); // txt.setText(imagePath.toString()); Bitmap bm = BitmapFactory.decodeFile(imagePath); uploadFile(imagePath.toString()); } } } public int uploadFile(String sourceFileUri) { String upLoadServerUri = "http://"+common.ipaddress+"/database/upload.php"; String fileName = sourceFileUri; int serverResponseCode = 0; 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()) { Log.e("uploadFile", "Source File Does not exist"); return 0; } try { // open a URL connection to the Servlet FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(upLoadServerUri); conn = (HttpURLConnection) url.openConnection(); // Open a HTTP // connection to // the URL conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy 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); dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); // create a buffer of // maximum size 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); // Responses from the server (code and message) serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn.getResponseMessage(); Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode); if (serverResponseCode == 200) { runOnUiThread(new Runnable() { public void run() { // tv.setText("File Upload Completed."); Toast.makeText(PhotosActivity.this, "File Upload Complete.", Toast.LENGTH_SHORT) .show(); } }); } // close the streams // fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { Toast.makeText(PhotosActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show(); Log.e("Upload file to server", "error: " + ex.getMessage(), ex); } catch (Exception e) { e.printStackTrace(); Toast.makeText(PhotosActivity.this, "Exception : " + e.getMessage(), Toast.LENGTH_SHORT).show(); Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e); } return serverResponseCode; } // UPDATED! public String getPath(Uri uri) { String[] projection = { MediaColumns.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); cursor.moveToFirst(); imagePath = cursor.getString(column_index); return cursor.getString(column_index); } }
and in php
$target_path1 = "upload/"; $target_path1 = $target_path1 . basename( $_FILES['uploaded_file']['name']); move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path1)
Hope that helps