单击按钮打开相机,应用程序崩溃

时间:2017-10-09 18:47:56

标签: java android android-intent camera

我正在使用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 ProcessRecord{5ce0ad0 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

1 个答案:

答案 0 :(得分:0)

在您的例外日志中,很明显您遇到了权限问题。

我希望您在Android Manifest文件中声明了Camera Permission,就像这样(在应用程序标记之外):

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

您不会要求用户提供相机权限。摄像机权限是一种危险的权限,因此,应该要求用户允许它。我还没有理解你的代码,但我相信以下是你的&#34;创建类&#34;看起来像是:

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

        }
    }
}