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 4613550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:34:05+00:00 2026-05-22T01:34:05+00:00

i doing app for uploading image to php server from android and the php

  • 0

i doing app for uploading image to php server from android and the php server return a url as response for the request. i checked no problem in php server side it works fine for iphone. but in android i cannot get response. i have checked the php server my image is not uploaded. i do not know what is the problem in the code and how to get response. Is there is any setting needed? my code :

 public class upload extends Activity {
 InputStream is;
 @Override
 public void onCreate(Bundle icicle) {
 super.onCreate(icicle);
 setContentView(R.layout.main);
 Bitmap bitmapOrg = BitmapFactory.decodeFile("/sdcard/imageq.png");
 ByteArrayOutputStream bao = new ByteArrayOutputStream();
 bitmapOrg.compress(Bitmap.CompressFormat.PNG, 90, bao);
 byte [] ba = bao.toByteArray();
 String ba1=Base64.encodeBytes(ba);
 ArrayList<NameValuePair> nameValuePairs = new
 ArrayList<NameValuePair>();
 nameValuePairs.add(new BasicNameValuePair("image",ba1));
 try{
  HttpClient httpclient = new DefaultHttpClient();
  HttpPost httppost = new HttpPost("http://xxxxxxxxxx/xxxxxx/upload.php");
  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  HttpResponse response = httpclient.execute(httppost);
  Log.e("uri",""+httppost.getURI());
  Log.e("response",""+response);
  HttpEntity entity = response.getEntity();
  is = entity.getContent();
  Log.e("is",""+is);
 }catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
 }
}
}

i get the above code from http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/
my log cat information:

05-11 10:09:39.488: ERROR/uri(1894): http://xxxxxxxxxx/xxxxxx/upload.php
05-11 10:09:39.488: ERROR/response(1894):    org.apache.http.message.BasicHttpResponse@44f73610
05-11 10:09:39.495: ERROR/is(1894): org.apache.http.conn.EofSensorInputStream@44f1fc88

i print the getURI it returns what i give in the httppost = new HttpPost(“….”). this is not real response from server. please help me.

  • 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-05-22T01:34:06+00:00Added an answer on May 22, 2026 at 1:34 am
    package com.telubi.connectivity;
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import android.util.Log;
    
    import com.cipl.TennisApp.Login;
    
    public class FileUploader {
    
        private String Tag = "UPLOADER";
        private String urlString;// = "YOUR_ONLINE_PHP";
        HttpURLConnection conn;
        String exsistingFileName;
        public String result;
    
        public String uploadImageData(String serverImageTag) {// Server image tag
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            try {
                // ------------------ CLIENT REQUEST
    
                Log.e(Tag, "Inside second Method");
    
                FileInputStream fileInputStream = new FileInputStream(new File(
                        exsistingFileName));
    
                // open a URL connection to the Servlet
    
                URL url = new URL(urlString);
    
                // Open a HTTP connection to the URL
    
                conn = (HttpURLConnection) url.openConnection();
    
                // Allow Inputs
                conn.setDoInput(true);
    
                // Allow Outputs
                conn.setDoOutput(true);
    
                // Don't use a cached copy.
                conn.setUseCaches(false);
    
                // Use a post method.
                conn.setRequestMethod("POST");
    
                conn.setRequestProperty("Connection", "Keep-Alive");
    
                conn.setRequestProperty("Content-Type",
                        "multipart/form-data;boundary=" + boundary);
    
                DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
    
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                if (serverImageTag.equalsIgnoreCase("courtImage")) {
                    dos.writeBytes("Content-Disposition: post-data; name=courtImage[];filename="
                            + exsistingFileName + "" + lineEnd);
                } else if (serverImageTag.equalsIgnoreCase("userImage")) {
                    dos.writeBytes("Content-Disposition: post-data; name=userImage[];filename="
                            + exsistingFileName + "" + lineEnd);
                }
                dos.writeBytes(lineEnd);
    
                Log.e(Tag, "Headers are written");
    
                // create a buffer of maximum size
    
                int bytesAvailable = fileInputStream.available();
                int maxBufferSize = 1000;
                // int bufferSize = Math.min(bytesAvailable, maxBufferSize);
                byte[] buffer = new byte[bytesAvailable];
    
                // read file and write it into form...
    
                int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
    
                while (bytesRead > 0) {
                    dos.write(buffer, 0, bytesAvailable);
                    bytesAvailable = fileInputStream.available();
                    bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
                }
    
                // send multipart form data necessary after file data...
    
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    
                String serverResponseMessage = conn.getResponseMessage();
                BufferedReader rd = new BufferedReader(new InputStreamReader(
                        conn.getInputStream()));
    
                // String serverResponseCode = conn.
                // String serverResponseMessage = conn.getResponseMessage();
    
                while ((result = rd.readLine()) != null) {
    
                    Log.v("result", "result " + result);
    
                        Login.fbResponse = result;
    
                }
                // close streams
                Log.e(Tag, "File is written");
                fileInputStream.close();
                dos.flush();
                dos.close();
                rd.close();
            } catch (MalformedURLException ex) {
                Log.e(Tag, "error: " + ex.getMessage(), ex);
            }
    
            catch (IOException ioe) {
                Log.e(Tag, "error: " + ioe.getMessage(), ioe);
            }
    
            // Parsing has finished.
            return result;
        }
    
        public FileUploader(String existingFileName, String urlString) {
    
            this.exsistingFileName = existingFileName;
            this.urlString = urlString;
    
        }
    
    }
    

    I am using this code to upload image from device to php server by post method in this code you find a FilsUploader constructor pass file name you want upload and destination url (PHP server Url) for uploading file .I hope this is help.

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

Sidebar

Related Questions

A quick question from a beginner. I'm doing an App for the iPhone and
I've started building an android app that will connect to a sql server and
I'm upgrading our app from rails 2.3.10 to 2.3.12, and in doing so had
I've been doing mobile app development for a long time (2001?), but the systems
We are doing an app to manage and print Word, Excel, PowerPoint, and PDF
I've had an app doing prefix searches for a while. Recently the index size
I'm doing a web app, and I need to make a branch for some
I am doing small Silverlight app which will allow users to Create orders. I
I was doing a sample app in ipad. I added about 4 to 5
Hey, im doing a little app for my smart phone, using Windows Mobile 6.

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.