Created
May 18, 2013 21:07
-
-
Save ma4a/5605790 to your computer and use it in GitHub Desktop.
Code to use HTTP PUT to send an image file (png in this case) to a webservice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static boolean putImage(Bitmap image) { | |
| ByteArrayOutputStream baos = null; | |
| HttpClient httpclient = new DefaultHttpClient(); | |
| httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, APP_UA); //UserAgent | |
| URI uri = null; | |
| try { | |
| uri = new URI("http://your.url.to.upload/the/file/via/PUT/123"); | |
| } catch (URISyntaxException e) { | |
| e.printStackTrace(); | |
| } | |
| HttpPut put = new HttpPut(uri); | |
| baos = new ByteArrayOutputStream(); | |
| image.compress(CompressFormat.PNG, 100, baos); //change this according your filetype | |
| byte[] data = baos.toByteArray(); | |
| try { | |
| baos.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| ByteArrayEntity image_byteArray = new ByteArrayEntity(data); | |
| image_byteArray.setContentType("image/png"); //change this according your filetype | |
| put.setEntity(image_byteArray); | |
| HttpResponse response = null; | |
| try { | |
| response = httpclient.execute(put); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| if (response.getStatusLine().getStatusCode() == 200) { | |
| Log.v("MyApplication", "PUT was successful"); | |
| return true; | |
| } else { | |
| Log.e("MyApplication", "PUT failed"); | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment