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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • 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
  • 8 Strategies To Accelerate Web Portal Development
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)

Trending

  • GenAI: Spring Boot Integration With LocalAI for Code Conversion
  • How To Compare DOCX Documents in Java
  • Mastering Serverless Debugging
  • Efficient Data Management With Offset and Cursor-Based Pagination in Modern Applications

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
Like (1)
Save
Tweet
Share
21.7K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 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
  • 8 Strategies To Accelerate Web Portal Development
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: