相机应用程序在startActivityForResult之后崩溃

时间:2017-03-11 04:57:23

标签: android android-studio

我正在构建一个简单的相机应用程序,在按下按钮时打开系统相机应用程序并在图像视图中显示它。但是应用程序在startActivityForResult方法上崩溃。

MainActivity.java:

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView imageView;
Button button;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
        Bundle extra = data.getExtras();
        Bitmap bitmap = (Bitmap) extra.get("data");
        imageView.setImageBitmap(bitmap) ;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.imageView);
    button = (Button) findViewById(R.id.button);
    if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY))
        button.setEnabled(true);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,REQUEST_IMAGE_CAPTURE);
        }
    });


}
}

log cat:

                                                                                Process: com.eldhoittangeorge.camera11032017, PID: 587
                                                                              java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=org.cyanogenmod.snap/com.android.camera.PhotoCamera } from ProcessRecord{f228cdc 587:com.eldhoittangeorge.camera11032017/u0a132} (pid=587, uid=10132) with revoked permission android.permission.CAMERA
                                                                                 at android.os.Parcel.readException(Parcel.java:1684)
                                                                                 at android.os.Parcel.readException(Parcel.java:1637)
                                                                                 at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3093)
                                                                                 at android.app.Instrumentation.execStartActivity(Instrumentation.java:1520)
                                                                                 at android.app.Activity.startActivityForResult(Activity.java:4225)
                                                                                 at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                                 at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                                 at android.app.Activity.startActivityForResult(Activity.java:4183)
                                                                                 at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                                                 at com.eldhoittangeorge.camera11032017.MainActivity$1.onClick(MainActivity.java:42)
                                                                                 at android.view.View.performClick(View.java:5639)
                                                                                 at android.view.View$PerformClick.run(View.java:22391)
                                                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6088)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2 个答案:

答案 0 :(得分:2)

点击按钮时调用活动拍照

private void activeTakePhoto() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 110);
        } else {
            takePicture();
        }

    }

    public void takePicture() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 22);
    }

并覆盖

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 110) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                takePicture();
            }
        }}

和清单应该有

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

为什么你应该遵循这个程序是因为从marshmallow开始,它必须在使用相机和访问厨房之前向用户请求自己的许可,无论你的清单中是否有权限。

答案 1 :(得分:1)

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView imageView;
Button button;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
        Bundle extra = data.getExtras();
        Bitmap bitmap = (Bitmap) extra.get("data");
        imageView.setImageBitmap(bitmap) ;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.imageView);
    button = (Button) findViewById(R.id.button);


    if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.CAMERA},110);
        takingPhoto();
    }
    else {
        takingPhoto();
    }
}
public void takingPhoto(){
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,REQUEST_IMAGE_CAPTURE);
        }
    });
}

}