Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 2.65 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android: upload photos
  2. public class Uploader{
  3.         public void upload(){
  4.             Log.i("EOH","hi...");
  5.             Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
  6.             photoPickerIntent.setType("image/*");
  7.             startActivityForResult(photoPickerIntent, 1);
  8.         }
  9.         protected void onActivityResult(int requestCode, int resultCode, Intent data)
  10.         {
  11.             if (resultCode == RESULT_OK)
  12.             {
  13.                 Bundle extras = data.getExtras();
  14.                 Bitmap b = (Bitmap) extras.get("data");
  15.                 //what do do now with this Bitmap...
  16.                     //how do I upload it?
  17.  
  18.  
  19.  
  20.  
  21.             }
  22.         }
  23.  
  24.     }
  25.        
  26. List<NameValuePair> params = new ArrayList<NameValuePair>(2);  
  27. params.add(new BasicNameValuePair("someValA", String.valueOf(lat)));  
  28. params.add(new BasicNameValuePair("someValB", String.valueOf(lng)));  
  29. new HttpConnection(handler).post("http://myurl.com/upload.php",params);
  30.        
  31. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  32.         {
  33.             if (resultCode == RESULT_OK)
  34.             {
  35.                 Uri imageUri=data.getData();
  36.                 List<NameValuePair> params = new ArrayList<NameValuePair>(1);
  37.                 params.add(new BasicNameValuePair("image", imageUri.getPath()));
  38.                 post("http://www.myurl.com/ajax/uploadPhoto.php",params);
  39.             }
  40.         }
  41.        
  42. public void post(String url, List<NameValuePair> nameValuePairs) {
  43.             HttpClient httpClient = new DefaultHttpClient();
  44.             HttpContext localContext = new BasicHttpContext();
  45.             HttpPost httpPost = new HttpPost(url);
  46.  
  47.             try {
  48.                 MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
  49.  
  50.                 for(int index=0; index < nameValuePairs.size(); index++) {
  51.                     if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
  52.                         // If the key equals to "image", we use FileBody to transfer the data
  53.                         entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
  54.                     }else{
  55.                         // Normal string data
  56.                         entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
  57.                     }
  58.                 }
  59.  
  60.                 httpPost.setEntity(entity);
  61.  
  62.                 HttpResponse response = httpClient.execute(httpPost, localContext);
  63.                 Log.i("EOH",response.toString());
  64.             } catch (IOException e) {
  65.                 e.printStackTrace();
  66.             }
  67.     }