我正在使用我正在开发的应用程序的相机功能将我的头撞在墙上。拍照并按下复选标记后,模拟器相机就会挂起并且不会返回到我的活动状态。选择X并取消它会返回活动。真的在寻找一些帮助。谢谢大家。
以下是onActivityResults()
方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/* If it's equal to my REQUEST_IMAGE_CAPTURE var, we are all good. */
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
selectedImage = imageUri;
mPipePicImage.setImageURI(imageUri);
}
}
这是一个用于创建ImageFile的辅助方法
private File createImageFile() throws IOException {
/* Create an image file name */
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "JPEG" + timeStamp + "_";
File storageDir = new File(obj.getFilesDir(), "images");
if (!storageDir.getParentFile().mkdir()) {
storageDir.getParentFile().mkdir();
}
File image = new File(storageDir,imageFileName);
/* Save a file: path for use with ACTION_VIEW intents */
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
使用此方法检查照片权限
private boolean checkPhotoPermissions() {
/* Checking if we already have permissions here.*/
if(ActivityCompat.checkSelfPermission(TallyActivity2.this,
android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(TallyActivity2.this,
Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { /* Added this for Contacts permissions */
/* Checking to see if we are equal to or at a higher version than Marshmallow */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
/* System default popup box here to ask for permissions. */
ActivityCompat.requestPermissions(TallyActivity2.this,
new String[]{android.Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
/* Added this today for trying to prompt for contacts*/
ActivityCompat.requestPermissions(TallyActivity2.this,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
return true;
} else {
/* Snapping a pic here. With this method.*/
dispatchTakePhoto();
}
return false;
}
dispatchTakePhoto()
方法
private void dispatchTakePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(takePictureIntent); // this worked originally
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
//Log.i(TAG,photoFile+" (6) in the dispatchTakePhoto() after init");
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, ""+e);
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(TallyActivity2.this,
"com.example.bigdaddy.pipelinepipetally.fileprovider", photoFile);
//takePictureIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
imageUri = Uri.fromFile(photoFile);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}