单击按钮打开相机,应用程序崩溃
Posted
技术标签:
【中文标题】单击按钮打开相机,应用程序崩溃【英文标题】:Open camera with clicking a button, app crashes 【发布时间】:2018-03-21 01:20:59 【问题描述】:我正在使用 API 26。我想在用户单击按钮后打开相机。我有按钮,它的 ID 是 CAMERA。当点击按钮时,我调用openCamera
方法。
public void openCamera(View v)
if(v.getId() == R.id.CAMERA)
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,0);
此方法在 MainActivity 类中,但按钮本身在其自己的名为“Create”的类中
// Create class
public class Create extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
我没有看到我做错了什么,因为该方法应该可以工作并且链接到按钮。
异常堆栈跟踪:
5906-5906/com.gif.gif E/AndroidRuntime: FATAL EXCEPTION: main Process: com.gif.gif, PID: 5906
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5336)
at android.view.View.performClick(View.java:6256)
.
.
.
.
Caused by: java.lang.SecurityException: Permission Denial: starting Intent
act=android.media.action.IMAGE_CAPTURE cmp=com.android.camera2/com.android.camera.CaptureActivity
from ProcessRecord5ce0ad0 5906:com.gif.gif/u0a84 (pid=5906, uid=10084)
with revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1888)
at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4365)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1610)
at android.app.Activity.startActivityForResult(Activity.java:4472)
MainActivity.java
package com.gif.gif;
import android.view.View;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.content.Intent;
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("gif", "onCreate triggered");
public void onButtonClick(View v)
if(v.getId() == R.id.CREATE)
Intent i = new Intent(MainActivity.this,create.class);
startActivity(i);
public void openCamera(View v)
if(v.getId() == R.id.CAMERA)
Intent intent = new
Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,0);
protected void onStart()
super.onStart();
Log.v("gif", "onStart triggered");
protected void onResume()
super.onResume();
Log.v("gif", "onResume triggered");
protected void onPause()
super.onPause();
Log.v("gif", "onPause triggered");
protected void onStop()
super.onStop();
Log.v("gif", "onStop triggered");
protected void onDestroy()
super.onDestroy();
Log.v("gif", "onDestory triggered");
我的 XML 代码位于 https://pastebin.com/M5BC8K9V
【问题讨论】:
当我在虚拟手机中运行它时,我得到的错误是“APP已停止” 请添加异常堆栈跟踪。您使用的是哪个 API?你有相机权限吗? 好的,这是相机权限的 XML 在AndroidStudio中,在底部,点击Android Monitor,您将看到已读取的文本行。复制它们并通过添加这些行来编辑问题。 能否请您也提供布局 xml。你设置相机权限了吗? 【参考方案1】:从您的异常日志中,很明显您遇到了权限问题。
我希望您在 Android 清单文件中声明了相机权限,如下所示(在应用程序标签之外):
<uses-permission android:name="android.permission.CAMERA" />
您没有向用户请求相机权限。相机权限是一个危险的权限,因此,应该要求用户允许它。我没有理解您的代码,但我相信以下是您的“创建类”的样子:
public class Create extends AppCompatActivity
public static final String TAG = Create.class.getSimpleName();
public static final int RC_CAMERA_PERMISSION = 1000;
@Override public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
// this layout has your CameraButton, right?
setContentView(R.layout.layout_create);
public void openCamera (View v)
if(v.getId() == R.id.CAMERA)
openCamera();
private void openCamera ()
// check for camera permission
int permissionCheck = ContextCompat.checkSelfPermission(Create.this, Manifest.permission.Camera);
// do we have camera permission?
if (permissionCheck == PackageManager.PERMISSION_GRANTED)
// we have camera permission, open System camera
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
else
// we don't have it, request camera permission from system
ActivityCompat.requestPermissions(thisActivity,
new String[]Manifest.permission.CAMERA,
RC_CAMERA_PERMISSION);
// result will come in this method, check if the permission was accepted?
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
// check request code for permissions
if(requestCode == RC_CAMERA_PERMISSION)
if(grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED)
// camera permission was granted
openCamera();
else
Log.d(TAG, "permissions not accepted");
【讨论】:
以上是关于单击按钮打开相机,应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章