Search code examples
androidandroid-imageandroid-networking

Android Send photo to server using retrofit post method


Hi guys first of all i am new to android and stack overflow.what i need is capture an image from camera and send to php server.

for capturing image i use

    Intent photo= new Intent("android.media.action.IMAGE_CAPTURE");
            startActivityForResult(photo, 0);

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0 && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
         imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
        delete.setVisibility(View.VISIBLE);
        button.setVisibility(View.INVISIBLE);
        image = ConvertBitmapToString(imageBitmap);

    }
}

//method to convert the selected image to base64 encoded string//////////////
public String ConvertBitmapToString(Bitmap bitmap){

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 25, byteArrayOutputStream);

    String encodedImage= null;
    try {
        encodedImage = URLEncoder.encode(Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return encodedImage;
}

my problem is the image is not visible in server.

server connection

      public interface SaveAPI {
     @FormUrlEncoded
    @POST("/example.php")
public void insertUser(
        @Field("username") String name,
        @Field("coname") String cname,
        @Field("location") String location,
        @Field("ddate") String date,
        @Field("dracc") String daccnt,
        @Field("cracc") String cccnt,
        @Field("amount") String samt,
        @Field("narr") String snarration,
        @Field("bill") String image,
        Callback<Response> callback);

     }
   private void insertUser() {
    //Here we will handle the http request to insert user to mysql db
    //Creating a RestAdapter
        RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(SAVE_URL) //Setting the Root URL
            .build();
       SaveAPI api = adapter.create(SaveAPI.class);
       api.insertUse r(
            name,
            sCname.getSelectedItem().toString(),
            loc,
            tdate,
            sdamount.getSelectedItem().toString(),
            scamount.getSelectedItem().toString(),
            amt.getText().toString(),
            narration.getText().toString(),
            image,
            new Callback<Response>() {
                @Override
                public void success(Response result, Response response) {
                    //On success we will read the server's output using 
            bufferedreader
                    //Creating a bufferedreader object
                    BufferedReader reader = null;

                    //An string to store output from the server
                    String output = "";

                    try {
                        //Initializing buffered reader
                        reader = new BufferedReader(new 
                    InputStreamReader(result.getBody().in()));

                        //Reading the output in the string
                        output = reader.readLine();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    //Displaying the output as a toast
                    Toast.makeText(ExpenseActivity.this, output, 
              Toast.LENGTH_LONG).show();
                }

                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(ExpenseActivity.this, error.toString(), 
               Toast.LENGTH_LONG).show();

                }

              }
          );
        }

Actually i am sending 9 data to the server,except image all are displayed in server anyone give solution to the problem.thanks in advance


Solution

  • first check whether that converted image as string is returning data and that response is a valid response u intented to get by again converting it into image.