DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Extensions: Architectural Deep-Dives into File Upload Security
  • Dynamic File Upload Component in Salesforce LWC
  • How To Protect Node.js Form Uploads With a Deterministic Threat Detection API
  • How To Get Cell Data From an Excel Spreadsheet Using APIs in Java

Trending

  • GenAI Implementation Isn't Magic — It’s a Lifecycle
  • Why Stable RAG Answers Can Still Hide Unstable Evidence
  • Building a Zero-Cost Approval Workflow With AWS Lambda Durable Functions
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Upload an Image to a Web Service Using HttpURLConnection

Learn how to upload an image from Android

By 
Nilanchala Panigrahy user avatar
Nilanchala Panigrahy
·
Aug. 17, 15 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
22.2K Views

Join the DZone community and get the full member experience.

Join For Free

The following code snippet can be used to upload an image to a 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.

Java
 
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);
}


Web Service Upload

Published at DZone with permission of Nilanchala Panigrahy. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Extensions: Architectural Deep-Dives into File Upload Security
  • Dynamic File Upload Component in Salesforce LWC
  • How To Protect Node.js Form Uploads With a Deterministic Threat Detection API
  • How To Get Cell Data From an Excel Spreadsheet Using APIs in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook