我有一个应用程序可以创建一个附加了相机的TextureView。在Moment中,Activity也是Application的MainLauncher。 我想按照预期使用新的Permission Style,并在使用Camera时调用Permission。但是,当我第一次启动应用程序时,它调用我的权限,我接受它,之后应用程序崩溃( Logcat中的唯一信息是SIG:9 )。当我再次启动它(因为我已经提交)它工作正常。我是否在错误的地方设置了Permission的调用? ( btw。我称之为多个权限,因为我也开始创建一个图像库)。我无法找到类似的问题所以我现在想问问题。
这是我的代码:
OnCreate中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callPermissions(); // calls multiple Permissions.
setContentView(R.layout.camera_landscape);
mCaptureButton = (Button) this.findViewById(R.id.photoButton);
mCalcButton = (Button) this.findViewById(R.id.descriptor);
mTrackButton = (Button) this.findViewById(R.id.track);
mTextureView = (AutoTextureView) findViewById(R.id.texture);
}
callPermissions:
private void callPermissions(){
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if(!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
permissionsNeeded.add("WRITE_EXTERNAL_STORAGE");
if(!addPermission(permissionsList, Manifest.permission.CAMERA))
permissionsNeeded.add("CAMERA");
if(permissionsList.size()> 0){
if(permissionsNeeded.size() > 0){
String message = "You need to grant access to " + permissionsNeeded.get(0);
for(int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(CameraTest.this, permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ALL_PERMISSIONS);
}
});
return;
}
ActivityCompat.requestPermissions(CameraTest.this, permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ALL_PERMISSIONS);
}
}
Overriden onRequestPermissionResult:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
switch(requestCode){
case REQUEST_CAMERA_PERMISSION:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
openCamera();
}else{
Toast.makeText(CameraTest.this, "CAMERA access Denied", Toast.LENGTH_SHORT).show();
}
break;
case REQUEST_CODE_ALL_PERMISSIONS:{
Map<String, Integer> perms = new HashMap<String, Integer>();
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
for(int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
if(perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
}else{
//Permissions Denied
Toast.makeText(CameraTest.this, "Some Permission is Denied", Toast.LENGTH_SHORT).show();
}
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
这里是LogCat输出:
10-25 10:56:56.906 26719-26719/? I/art: Late-enabling -Xcheck:jni
10-25 10:56:56.926 26719-26725/? I/art: Debugger is no longer active
10-25 10:56:57.084 26719-26743/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-25 10:56:57.163 26719-26743/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb
10-25 10:56:57.172 26719-26743/? I/OpenGLRenderer: Initialized EGL, version 1.4
10-25 10:56:57.331 26719-26719/? I/CameraManagerGlobal: Connecting to camera service
10-25 10:57:02.407 26719-26719/com.example.camera D/AndroidRuntime: Shutting down VM
10-25 10:57:02.408 26719-26866/com.example.camera I/Process: Sending signal. PID: 26719 SIG: 9
答案 0 :(得分:0)
我可以自己解决这个问题,
我也有一个方法(我完全错过了添加)哪个被称为openCamera
哪个为TextureSurface配置相机:
private void openCamera() {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if(!ActivityCompat.shouldShowRequestPermissionRationale(CameraTest.this, Manifest.permission.CAMERA)){
//showMessageOKCancel("You need to allow access to Camera", new DialogInterface.OnClickListener(){
// @Override
// public void onClick(DialogInterface dialog, int which){
// ActivityCompat.requestPermissions(CameraTest.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
// }
//});
return;
}
ActivityCompat.requestPermissions(CameraTest.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
return;
}
cameraManager.openCamera(mCameraId, mDeviceCallback, mBackgroundHandler);
}catch(CameraAccessException e) {
e.printStackTrace();
}
}
在那里,当用户尚未授予权限时,我也启动了一个Dialog。在我评论showMessageOkCancel方法(创建对话框)后,一切正常,只有我的callPermissions
方法请求我的两个权限,并且应用程序在第一次启动时不会崩溃。