【发布时间】:2017-03-24 09:18:30
【问题描述】:
我只是想制作一个使用运行时权限的简单相机应用程序......它似乎在第一次启动应用程序时加载良好(请求相机权限)。允许访问后,它可以工作......但是一旦我关闭它并重新启动它,它只会显示一个白色图像,我的图标不会响应。我已经在应用程序中手动检查了权限,并且相机仍然被授予访问权限,但我认为我搞砸了我的权限代码。
这是 MainActivity 代码:
public class MainActivity extends AppCompatActivity {
private static final int MY_PERMISSIONS_REQUEST_CAMERA = 1;
private Camera mCamera = null;
private Camera mCameraFront = null;
private CameraView mCameraView = null;
public int switchCamera = 1;
// int permissionCheck = ContextCompat.checkSelfPermission(this,
// Manifest.permission.CAMERA);
// String[] perms = {"android.permission.CAMERA"};
// int permsRequestCode = 200;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
try {
mCamera = Camera.open(1);//you can use open(int) to use different cameras
} catch (Exception e) {
Log.d("ERROR", "Failed to get camera: " + e.getMessage());
}
SwapCamera();
// if (mCamera != null) {
//// mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
//// FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
//// camera_view.addView(mCameraView);//add the SurfaceView to the layout
// SwapCamera();
// }
//btn to close the application
ImageButton imgClose = (ImageButton) findViewById(R.id.imgClose);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCamera.setPreviewCallback(null);
mCamera.setErrorCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
System.exit(0);
}
});
// btn to switch camera
ImageButton imgSwitch = (ImageButton) findViewById(R.id.cameraSwitch);
imgSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// switchCamera++;
}
});
}
}
}
public void SwapCamera() {
mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
camera_view.addView(mCameraView);//add the SurfaceView to the layout
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// camera-related task you need to do.
try{
mCamera = Camera.open(1);//you can use open(int) to use different cameras
} catch (Exception e){
Log.d("ERROR", "Failed to get camera: " + e.getMessage());
}
if(mCamera != null) {
// mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
// FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
// camera_view.addView(mCameraView);//add the SurfaceView to the layout
SwapCamera();
}
//btn to close the application
ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCamera.setPreviewCallback(null);
mCamera.setErrorCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
System.exit(0);
}
});
// btn to switch camera
ImageButton imgSwitch = (ImageButton)findViewById(R.id.cameraSwitch);
imgSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// switchCamera++;
}
});
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
public void onActivityResult() {
}
}
【问题讨论】:
标签: android camera android-camera runtime-permissions