I found a good example for uploading an image to a server.
- create a bitmap variable before you do anything.
- to set the image name to the SD card.
- this variable, you must put the path to the file, this is for you.
- sendData is the name of the function to call it, you can use something like
sendData(null) . - Remember to wrap it in a try catch.
private Bitmap bitmap; public static String exsistingFileName = ""; public void sendData(String[] args) throws Exception { try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); // here, change it to your php; HttpPost httpPost = new HttpPost("http://www.myURL.com/myPHP.php"); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); bitmap = BitmapFactory.decodeFile(exsistingFileName); // you can change the format of you image compressed for what do you want; // now it is set up to 640 x 480; Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 480, true); ByteArrayOutputStream bos = new ByteArrayOutputStream(); // CompressFormat set up to JPG, you can change to PNG or whatever you want; bmpCompressed.compress(CompressFormat.JPEG, 100, bos); byte[] data = bos.toByteArray(); // sending a String param; entity.addPart("myParam", new StringBody("my value")); // sending a Image; // note here, that you can send more than one image, just add another param, same rule to the String; entity.addPart("myImage", new ByteArrayBody(data, "temp.jpg")); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, localContext); BufferedReader reader = new BufferedReader(new InputStreamReader( response.getEntity().getContent(), "UTF-8")); String sResponse = reader.readLine(); } catch (Exception e) { Log.v("myApp", "Some error came up"); } }
source share