1

I'm making a simple application that uses the camera intent to take a picture, and then converts the picture to a base64 string and sends it to my server along with an image name. I'm not sure how to send the image using Retrofit, the concept is a bit unclear to me and does not make sense as I'm new to Java/Android App Development.

This is what I have so far. I have looked at several tutorials but I'm really unsure as to where to add the Retrofit code and how to make it work. I have also included the PHP code on my server. Thanks

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    private ImageButton ImageButton;
    private String encoded_string, image_name;
    private File file;
    private Uri file_uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageButton = (ImageButton) findViewById(R.id.camera);
        ImageButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                i.putExtra(MediaStore.EXTRA_OUTPUT,file_uri);
                startActivityForResult(i,10);
            }
        });
    }

    private void getFileUri() {
        image_name = "testing1234.jpeg";
        file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), image_name);
        file_uri = Uri.fromFile(file);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode == 10 && resultCode == RESULT_OK) {
        }
    }
}

PHP

<?php
header('Content-type : bitmap; charset=utf-8');

if(isset($_POST["encoded_string"])){
  $encoded_string = $_POST["encoded_string"];
  $device_name = $_POST["device_name"];

  /*$image_name = $device_name.'.jpg';*/
  $decoded_string = base64_decode($encoded_string);

  $path = 'images/'.$image_name;
  $file = fopen($path, 'wb');
  $is_written = fwrite($file, $decoded_string);
  fclose($file);

  $extracted = shell_exec("python test.py $image_name");
  echo $extracted;

 }

else {
  echo "Failed :(";
 }

?>
Pravin Divraniya
  • 4,223
  • 2
  • 32
  • 49
0248881
  • 731
  • 1
  • 4
  • 21
  • http://www.androidhive.info/2016/05/android-working-with-retrofit-http-library/ see this tutorial . – anddevmanu Sep 08 '16 at 09:02
  • @anddevmanu Is there something similar except with POST instead of GET? Theres so many tutorials about GET, but nothing too helpful about POST – 0248881 Sep 08 '16 at 09:05
  • I think you need to look at this question: http://stackoverflow.com/questions/22787585/android-retrofit-base64-body. Does it answer what you need? – Malcolm Sep 08 '16 at 09:22
  • 1
    there are numerous example you can find on google. https://www.sitepoint.com/retrofit-a-simple-http-client-for-android-and-java/ – anddevmanu Sep 08 '16 at 09:23

0 Answers0