无法从可点击的 TextView 打开相机
Cannot open camera from clickable TextView
我在用我的代码打开相机时遇到困难,我希望得到一些帮助...基本上应用程序会显示,当我点击文本时,应用程序会立即关闭...我觉得我不是正确连接两者。
这是来自 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
可能您没有在清单中添加相机权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
给你..
1.Please 如果您也在 activity 中使用 onClick 侦听器,请从 xml 文件中删除您的 onClick 侦听器。
<TextView
android:id="@+id/textView4"
android:clickable="true"
android:text="@string/openCamera" />
2.Add 未获得您的 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 在您的 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 要显示捕获的图像,需要在 xml 文件中添加 imageview。要显示图像,请在 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 下面是 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);
}
}
我在用我的代码打开相机时遇到困难,我希望得到一些帮助...基本上应用程序会显示,当我点击文本时,应用程序会立即关闭...我觉得我不是正确连接两者。
这是来自 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
可能您没有在清单中添加相机权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
给你..
1.Please 如果您也在 activity 中使用 onClick 侦听器,请从 xml 文件中删除您的 onClick 侦听器。
<TextView
android:id="@+id/textView4"
android:clickable="true"
android:text="@string/openCamera" />
2.Add 未获得您的 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 在您的 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 要显示捕获的图像,需要在 xml 文件中添加 imageview。要显示图像,请在 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 下面是 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);
}
}