How do I open the android camera and take the photo and upload the photo to the server?

Asked

Viewed 4,956 times

7

I have a problem in the application it opens the camera but does not upload hold can see where I am missing

follows 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);
           }
       });
   }
}
  • Dude, take a look at this article. http://www.thiengo.com.br/utilizando-a-camera-do-smartphone-em-sua-app-android

  • There is nothing in this codex to upload. I think I’m missing something.

  • I think you could split it up into two questions.

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 to a WS.

You need the following permissions to work correctly

<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 (int normal, can be 1, 2, 10, 35, etc.) to identify and treat the camera’s feedback, you pass this information in the Activity call and then use this same code to validate the image’s return

Then you will need to treat the return, otherwise you will not be able to save the image or 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);
        }
    }
}
  • Thiago in this part of the code that you sent ibt_foto.setImageBitmap(imageBitmap); the ibt_foto would be the id on the button and the path has to declare because the error cannot solve the symbol

  • Hi, I forgot to change the "path" there, but it is the path+file name, in the above example is the Environment.getExternalStorageDirectory() + "/arquivo.jpg" as for ibt_photo, it is an Imagebutton, which after taking the photo I update the image of the button with the photo that was taken. I edited the original post and changed the "path" path to avoid error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.