我有一個選項供用戶在我的活動中拍照。這是我的代碼相同:Android M相機安全異常
int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private static final int REQUEST_SELFIE = 123;
private static String[] PERMISSIONS_SELFIE = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
LinearLayout selfie = (LinearLayout) findViewById(R.id.selfie);
selfie.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((ContextCompat.checkSelfPermission(SelfieActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) &&
(ContextCompat.checkSelfPermission(SelfieActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
&& (ContextCompat.checkSelfPermission(SelfieActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
requestCameraAndExternalPermission();
} else {
takeSelfie();
}
}
});
private void requestCameraAndExternalPermission() {
ActivityCompat.requestPermissions(this, PERMISSIONS_SELFIE, REQUEST_SELFIE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_SELFIE) {
if (grantResults.length == 3 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED
&& grantResults[2] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
takeSelfie();
} else {
Toast.makeText(getApplicationContext(), "Some Permissions Denied", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == REQUEST_ALBUM) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
openGallery();
} else {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void takeSelfie() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
if (file.exists()) {
file.delete();
file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
File destination = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
imagePath = destination.getAbsolutePath();
Intent intent = new Intent(SelfieActivity.this, PhotoAdjustActivity.class);
intent.putExtra("path", imagePath);
startActivity(intent);
} else if (requestCode == SELECT_FILE && data != null) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null,
null);
assert cursor != null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
if (imagePath == null) {
imagePath = getActualPathFromUri(selectedImageUri);
}
Intent intent = new Intent(SelfieActivity.this, PhotoAdjustActivity.class);
intent.putExtra("path", imagePath);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
overridePendingTransition(0, 0);
cursor.close();
}
}
}
當我檢查我的手機(聯想K3注)時,它工作正常。但是今天我在摩托羅拉(Android 6)遇到了一個崩潰。這是崩潰:
Fatal Exception: java.lang.SecurityException
Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.motorola.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/image.jpg} (has extras) } from ProcessRecord{735d251 15134:com.galleri5.android/u0a149} (pid=15134, uid=10149) with revoked permission android.permission.CAMERA
這裏是我的清單:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.camera.autofocus"
android:required="false"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
什麼能這樣做的原因,我應該怎麼解決這個問題?
檢查此鏈接http://developer.android.com/intl/es/training/permissions/requesting.html –
@MiguelBenitez我有檢查了。 –
使用設置檢查應用程序的許可,也許您之前撤銷。 –