how to open the android camera and take the photo and upload the photo to the server?

7

I have a problem with the application it opens the camera but it does not upload aguem can see where I'm going wrong

follow the code

public class MainActivity extends Activity{

   //chamar quando a atividade é a primeira criada
   @Override
    public void onCreate(Bundle savedIntanceState){
       super.onCreate(savedIntanceState);
       setContentView(R.layout.activity_main);

       Button foto = (Button) findViewById(R.id.buttonToFoto);

       Button video = (Button) findViewById(R.id.buttonToVideo);

       foto.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivity(intent);

               String arquivo = Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".jpg";

               File file = new File(arquivo);

               Uri outputFileUri = Uri.fromFile(file);

               intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

           }
       });

       video.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent (MediaStore.ACTION_VIDEO_CAPTURE);

               startActivity(intent);

               String arquivo = Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis();

               File file = new File(arquivo);

               Uri outputFileUri = Uri.fromFile(file);

               intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
           }
       });
   }
}
    
asked by anonymous 13.10.2016 / 21:42

1 answer

2

You can open the camera automatically using the correct intent, Android will open the camera screen alone and you can treat the photo the way you want, save locally, convert to base64, etc. and then send it to a WS.

You need the following permissions to work properly

<permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Example to open the camera

File file = new File(Environment.getExternalStorageDirectory() + "/arquivo.jpg");
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is a standard code (normal int, it can be 1, 2, 10, 35, etc.) to identify and treat the camera return, you pass this information on the activity call and then use that same code to validate the return image

Then you need to handle the return, otherwise you will not be able to save the image nor convert, in the example below it treats the return and converts the image to base64

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    System.gc(); // garbage colector
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                try {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 3;
                    Bitmap imageBitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/arquivo.jpg", options);

                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    boolean validaCompressao = imageBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
                    byte[] fotoBinario = outputStream.toByteArray();

                    String encodedImage = Base64.encodeToString(fotoBinario, Base64.DEFAULT);

                    ibt_foto.setImageBitmap(imageBitmap); // ImageButton, seto a foto como imagem do botão

                    boolean isImageTaken = true;
                } catch (Exception e) {
                    Toast.makeText(this, "Picture Not taken",Toast.LENGTH_LONG).show();e.printStackTrace();
                }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Picture was not taken 1 ", Toast.LENGTH_SHORT);
        } else {
            Toast.makeText(this, "Picture was not taken 2 ", Toast.LENGTH_SHORT);
        }
    }
}
    
14.10.2016 / 00:09