1

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

karthi
  • 183
  • 1
  • 3
  • 14
  • ConvertBitmapToString return some data or null? – an_droid_dev Aug 22 '17 at 09:47
  • You first need to check where is the problem. It can be either that you don't get the image correctly, that the image is not converted correctly or your call to the server has an error. Once you'd know that you'll either be able to find the solution yourself or ask for help. – Eselfar Aug 22 '17 at 09:50
  • yeah buddy return some data but it not displayed........... – karthi Aug 22 '17 at 10:05
  • i think i have a problem in image conversation...how to slove this – karthi Aug 22 '17 at 10:12

3 Answers3

1

Now my image conversation works perfectly........

    public String ConvertBitmapToString(Bitmap bitmap)throws 
    FileNotFoundException {
    BitmapFactory.Options options = null;
    options = new BitmapFactory.Options();
    options.inSampleSize = 3;
    ByteArrayOutputStream byteArrayOutputStream = new 
    ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,40, byteArrayOutputStream);
    byte[] byteArrayImage = byteArrayOutputStream.toByteArray();
    String encodedImage = 
    Base64.encodeToString(byteArrayImage,Base64.DEFAULT);
    return encodedImage;
   }
karthi
  • 183
  • 1
  • 3
  • 14
0

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.

A2N
  • 150
  • 2
  • 13
  • did you checked this post for conversion to and back:https://stackoverflow.com/questions/41396194/how-to-convert-image-to-string-in-android – A2N Aug 22 '17 at 10:56
  • try those 2 ways to fetch a img uri : https://stackoverflow.com/questions/8296182/how-to-get-the-uri-of-a-image-stored-on-the-sdcard?rq=1 – A2N Aug 23 '17 at 10:25
  • if you were picking up from gallery, obviously you can get its file path uri. – A2N Aug 23 '17 at 10:26
0

first of all, debug the app, and check if the image converts into base64. If it is converting correctly and you send it to your server, the problem is in your server server. Debug please your project and tell what is happening. Pot please the part of the server code where you bind the data

John P
  • 1,159
  • 2
  • 18
  • 44
  • bro i have a problem in image conversation,but i got the same image conversation code in all internet sites. – karthi Aug 22 '17 at 12:09
  • check out this tutorial. https://www.youtube.com/watch?v=e8x-nu9-_BM I used it about 2 years ago and everything worked – John P Aug 22 '17 at 15:41