无法从可单击的文本视图中打开相机



我用我的代码打开相机时遇到困难,我将不胜感激......基本上应用程序将显示,当我单击文本时,该应用程序会立即关闭......我觉得我没有正确地将两者联系起来。

以下是activity_main.xml的代码:

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

这是我的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提到了以下内容:

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

你去吧。

1.如果您在活动中也使用 onClick 侦听器,请从 xml 文件中删除您的 onClick 侦听器。

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

2.将以下权限添加到您的安卓清单.xml

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

3.在您的活动中添加下面的点击监听器。

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

5.如果要显示捕获的图像,则需要在xml文件中添加图像视图。要显示图像,请在您的活动中使用以下代码。

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.在创建后添加以下方法。

@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);
    }
    }

您可能错过了在清单中添加相机权限:

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

最新更新