0
votes

I'm building an application in Android Studio for my University class. It asks to upload pictures from my device, and I was thinking about trying taking also photos from the camera. I have to use only the Android Studio Emulator and not a real device, so, I'm not sure I can do it.

I solved as this:

Integer REQUEST_CAMERA = 1;
[...]
button_scatta.setOnClickListener(new View.OnClickListener() {

  public void onClick(View v) {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CAMERA);

  }

});

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK){

        if (requestCode == REQUEST_CAMERA) {

            Bundle bundle = data.getExtras();
            final Bitmap bmp = (Bitmap) bundle.get("data");
            ivImage.setImageBitmap(bmp);

        }[...]
    }
}

The Manifest has the following uses-permissions:

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

I had this security error: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.android.camera2/com.android.camera.CaptureActivity } from ProcessRecord{6d2a42a 7516:com.example.punta.geopost/u0a85} (pid=7516, uid=10085) with revoked permission android.permission.CAMERA

2

2 Answers

0
votes

Add following permission in Manifest file

<uses-feature
    android:name="android.hardware.camera.any"
    android:required="true" />
<uses-feature
    android:name="android.hardware.camera.autofocus"
    android:required="false" />

Also, you need to implement Runtime Permission refer developer page, example 1 and example 2

0
votes

Please put run time permission for accessing camera because as per https://developer.android.com you have to put runtime permission on or after device supports marshmallow.