Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:53:30+00:00 2026-06-13T04:53:30+00:00

I have a simple PHP service I’m trying to hit from an Android app,

  • 0

I have a simple PHP service I’m trying to hit from an Android app, and I would like to pass a raw image via a POST param. I have a PHP/curl script working which does the following:

$url = "http://myphp.php"
$imagefilepath = 'path_to_png_file.png';
$imagedata = file_get_contents($imagefilepath);
$data = array('imagedata' => $imagedata);
// a few other fields are set into $data, but not important

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
curl_exec($ch);

I want to mimic this very thing in Java from my Android app, using AsyncHttpClient (http://loopj.com/android-async-http/).

Sounds simple enough, and I can get the call working in Java, but the issue is that the data I send is not recognized on the other end as an image. With the above PHP/Curl script, however, it works fine on all fronts.

Here is my Java code, with a few commented-out things I’ve tried:

String photoFilePath = "path_to_my_photo_on_disk.jpg";

Bitmap bm = BitmapFactory.decodeFile(photoFilePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] byteArrayPhoto = baos.toByteArray();

AsyncHttpClient client = new AsyncHttpClient(context);
RequestParams params = new RequestParams();
try {
      // THINGS I HAVE TRIED (AND FAILED):
      //params.put("imagedata", new File(photoFilePath));
      //params.put("imagedata", new ByteArrayInputStream(byteArrayPhoto), "photo.jpg");                        
      //params.put("imagedata", new String(byteArrayPhoto));
      //params.put("imagedata", new String(byteArrayPhoto, "UTF-8"));
      //params.put("imagedata", fileToString(photoFilePath));
      //params.put("imagedata", new FileInputStream(new File(photoFilePath)), "photo.jpg", "image/jpeg");
} catch (Exception e) {
      e.printStackTrace();
}

client.post(context, myURL, params, new AsyncHttpResponseHandler() {
...override methods, onSuccess() is called...
}

// for reference, for the above-called method:
private String fileToString(String filename) throws IOException
    {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        StringBuilder builder = new StringBuilder();
        String line;

        // For every line in the file, append it to the string builder
        while((line = reader.readLine()) != null)
        {
            builder.append(line);
        }

        return builder.toString();
    }
  • Note that on a few of the commented-out lines/attempts above, the POST parameter doesn’t even make it across to the other side.

I’ve also tried a few other ways to get the file into a byte-array, as well as encoding the file (base64), with no luck. For whatever reason, the call is successful and data gets transferred, but each time, when we try to open the image on the server side, it’s corrupt and/or won’t open as a JPG. I’ve tried small and large image files.

I have definitely done research and tried many solutions I have found, but nothing seems to be working. I’m sure I’m missing something obvious here, but can anyone steer me in the right direction on this?

Any help would be GREATLY appreciated!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:53:31+00:00Added an answer on June 13, 2026 at 4:53 am

    The answer to this was fairly simple. As it turns out, I did need to base64-encode the image, as it was simply getting corrupt somehow when sending to the service. I did this via this code:

    String encodedPhotoStr = Base64.encodeToString(byteArrayPhoto, Base64.DEFAULT);
    params.put("imagedata", encodedPhotoStr);
    

    But it was vital on the PHP side to have a line added in order to decode this data as follows:

    $raw_data_str = base64_decode($data_str_from_java)
    

    This fixed the issue and the image is now viewable after being sent. So for whatever reason, sending raw binary/image data from java into the PHP just would not work (despite the curl script working fine), and I absolutely had to encode and decode it this way to get it to work from the Java code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple PHP app that prints 'hello world'. When I run it
I have a simple php page that displays data from a mysql database. I
Here I have a simple php script which displays some values from a database
I have a simple query in php on oracle: $query='select u.username,u.lastname,u.firstname,c.event,c.reason from users u,
I would like to consume WCF service (created by ASP .NET) with PHP. I
i have created a simple web service using Php Nusoap. its working correctly but
I have simple php validation form that is halfway working. If you leave the
I have a simple php script on my domain that sends me an email:
I have a simple PHP script that will either serve up a streaming ASF
How to manually create Friendly URLs? (PHP) So I have created simple php file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.