Tutorials Android Upload image to a web service using HttpURLConnection

Upload image to a web service using HttpURLConnection

The following code snippet can be used to upload an image to web service in Android. After getting a Bitmap object from the camera or other source, you can compress the create an HttpURLConnection and attach the image to the request body.

try {
	URL url = new URL(SERVER_POST_URL);
	HttpURLConnection c = (HttpURLConnection) url.openConnection();
	c.setDoInput(true);
	c.setRequestMethod("POST");
	c.setDoOutput(true);
	c.connect();

	OutputStream output = c.getOutputStream();
	bitmap.compress(CompressFormat.JPEG, 50, output);
	output.close();

	Scanner result = new Scanner(c.getInputStream());
	String response = result.nextLine();
	Log.e("ImageUploader", "Error uploading image: " + response);

	result.close();
} catch (IOException e) {
		Log.e("ImageUploader", "Error uploading image", e);
}
Total
0
Shares
guest
0 Comments
Inline Feedbacks
View all comments
Previous Post

Mac OS X Window Screenshot Hacks

Next Post

How to add zoom controls to Android MapView

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
0
Would love your thoughts, please comment.x
()
x