使用带有PHP的Json StringRequest将图像从Android中的库上传到服务器?

I want to upload image to server, What can I do to save image in server.

I want to use String request of Json to pass the value of binary file image to server. At server side I use php to save file like this:

            $binary=base64_decode($base);
  header('Content-Type: bitmap; charset=utf-8');
   // Images will be saved under 'www/imgupload/uplodedimages' folder
      $file = fopen('../uploadedimages/'.$filename, 'wb');
      // Create File

     fwrite($file, $binary);
     fclose($file);

Convert the selected Image into String using Base64 Utility Class. Use AsynTask to encode Image to String since it is long running process. Once encoding is done, call ‘triggerImageUpload’ method which will initiate Image uploading.

public void encodeImagetoString() {
        new AsyncTask<Void, Void, String>() {

        protected void onPreExecute() {

        };

        @Override
        protected String doInBackground(Void... params) {
            BitmapFactory.Options options = null;
            options = new BitmapFactory.Options();
            options.inSampleSize = 3;
            bitmap = BitmapFactory.decodeFile(imgPath,
                    options);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // Must compress the Image to reduce image size to make upload easy
            bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream); 
            byte[] byte_arr = stream.toByteArray();
            // Encode Image to String
            encodedString = Base64.encodeToString(byte_arr, 0);
            return "";
        }

        @Override
        protected void onPostExecute(String msg) {
            prgDialog.setMessage("Calling Upload");
            // Put converted Image string into Async Http Post param
            params.put("image", encodedString);
            // Trigger Image upload
            triggerImageUpload();
        }
    }.execute(null, null, null);
}

Now Post Encoded String to through HTTP to PHP server using AsyncHttp library. (you can use any HTTP library). Method ‘triggerImageUpload’ gets called in onPostExecute method when Encoding is complete:

public void triggerImageUpload() {
        makeHTTPCall();
    }

Post Encoded String to PHP server:

public void makeHTTPCall() {
    prgDialog.setMessage("Invoking Php");        
    AsyncHttpClient client = new AsyncHttpClient();
    // Don't forget to change the IP address to your LAN address. Port no as well.
    client.post("http://192.168.2.5:9000/imgupload/upload_image.php",
            params, new AsyncHttpResponseHandler() {
                // When the response returned by REST has Http
                // response code '200'
                @Override
                public void onSuccess(String response) {
                    // Hide Progress Dialog
                    prgDialog.hide();
                    Toast.makeText(getApplicationContext(), response,
                            Toast.LENGTH_LONG).show();
                }

                // When the response returned by REST has Http
                // response code other than '200' such as '404',
                // '500' or '403' etc
                @Override
                public void onFailure(int statusCode, Throwable error,
                        String content) {
                    // Hide Progress Dialog
                    prgDialog.hide();
                    // When Http response code is '404'
                    if (statusCode == 404) {
                        Toast.makeText(getApplicationContext(),
                                "Requested resource not found",
                                Toast.LENGTH_LONG).show();
                    }
                    // When Http response code is '500'
                    else if (statusCode == 500) {
                        Toast.makeText(getApplicationContext(),
                                "Something went wrong at server end",
                                Toast.LENGTH_LONG).show();
                    }
                    // When Http response code other than 404, 500
                    else {
                        Toast.makeText(
                                getApplicationContext(),
                                "Error Occured 
 Most Common Error: 
1. Device not connected to Internet
2. Web App is not deployed in App server
3. App server is not running
 HTTP Status code : "
                                        + statusCode, Toast.LENGTH_LONG)
                                .show();
                    }
                }
            });
}

Now Based on the HTTP response returned by PHP server, inform User about the upload status (Uploaded successfully or Failed).

wow we have done it.