0

I am having difficulty opening the camera with my code and I would appreciate some help... Basically the app will display and when I click on the text, the app closes straight away... I feel I am not linking the two correctly.

Here is the code from activity_main.xml:

  <TextView
    android:id="@+id/textView4"
    android:clickable="true"
    android:onClick="onClick"
    android:text="@string/openCamera" />

And here is my Java:

public class MainActivity extends AppCompatActivity {

static final int REQUEST_IMAGE_CAPTURE = 1;
TextView txt;

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

    txt = (TextView) findViewById(R.id.textView4);

    if (!hasCamera()) {
        txt.setEnabled(false);
    }

    txt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

        }
    });
}

private boolean hasCamera() {
    return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);

}

Logcat mentions the following:

FATAL EXCEPTION: main
                                                                                                                              java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.sec.android.app.camera/.Camera launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } } from ProcessRecord{29106f2 12844:com.example.xxxxxxxxxx} (pid=12844, uid=10188) with revoked permission android.permission.CAMERA
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
ilikecats
  • 11
  • 4

2 Answers2

0

Probably you have missed adding camera permission in your manifest :

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />
Nawrez
  • 3,314
  • 8
  • 28
  • 42
0

Here you go..

1.Please remove your onClick listener from xml file if you are using onClick listener in activity also.

<TextView
    android:id="@+id/textView4"
    android:clickable="true"
    android:text="@string/openCamera" />

2.Add below permission to your AndroidManifest.xml

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

3.Add below click listener in your activity.

textView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivity(cameraIntent);
            }
        });

5.If you want to show captured image, you need to add imageview in your xml file. To show image please use below code in your activity.

textView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, 1);
            }
        });

6.Add below method after onCreate.

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        Bitmap image = (Bitmap) data.getExtras().get("data");
        ImageView imageview = (ImageView) findViewById(R.id.imageView); //sets imageview as the bitmap
        imageview.setImageBitmap(image);
    }
    }
Flutterian
  • 1,761
  • 1
  • 21
  • 47