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
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • Top 9 Tools To Take Full Page Website Screenshots
  • 8 Strategies To Accelerate Web Portal Development
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)
  • How To Scan and Validate Image Uploads in Java

Trending

  • Scaling SRE Teams
  • CI/CD Metrics You Should Be Monitoring
  • Ways To Reduce JVM Docker Image Size
  • The Most Valuable Code Is the Code You Should Not Write

Upload an Image to a Web Service Using HttpURLConnection

Learn how to upload an image from Android

Nilanchala Panigrahy user avatar by
Nilanchala Panigrahy
·
Aug. 17, 15 · Tutorial
Like (1)
Save
Tweet
Share
21.5K 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

  • Top 9 Tools To Take Full Page Website Screenshots
  • 8 Strategies To Accelerate Web Portal Development
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)
  • How To Scan and Validate Image Uploads in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • 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: