So, I have below code that open camera, capture the image and save it on SDCard.
public void getPhotoFromCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory()
+ File.separator
+ getString(R.string.directory_name_corp_chat)
+ File.separator
+ getString(R.string.directory_name_temp)
);
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
try {
mediaFile = File.createTempFile(
"TEMP_FULL_IMG_" + timeStamp,
".jpg",
mediaStorageDir
);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mediaFile));
startActivityForResult(takePictureIntent, PICK_FROM_CAMERA);
} catch (IOException e) {
e.printStackTrace();
}
}
private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
// retrieve data on return
cropIntent.putExtra("return-data", true);
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory()
+ File.separator
+ getString(R.string.directory_name_corp_chat)
+ File.separator
+ getString(R.string.directory_name_temp)
);
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
try {
croppedFile = File.createTempFile(
"TEMP_CROPPED_IMG_" + timeStamp,
".jpg",
mediaStorageDir
);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(croppedFile));
startActivityForResult(cropIntent, PIC_CROP);
} catch (IOException e) {
e.printStackTrace();
}
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_CAMERA) {
if (resultCode == RESULT_OK) {
performCrop(Uri.fromFile(mediaFile));
} else {
Log.i("Camera", "result cancel. Hence, deleting file: " + mediaFile.getPath());
Log.i("File deleted ", mediaFile.delete() + "");
}
}
if (requestCode == PICK_FROM_GALLERY) {
if (resultCode == RESULT_OK) {
performCrop(data.getData());
} else {
Log.i("Gallery", "result cancel");
}
}
if (requestCode == PIC_CROP) {
if (resultCode == RESULT_OK) {
imageView.setImageBitmap(BitmapFactory.decodeFile(croppedFile.getAbsolutePath()));
if (mediaFile != null) {
Log.i("Camera", "result cancel. Hence, deleting file: " + mediaFile.getPath());
Log.i("File deleted ", mediaFile.delete() + "");
}
} else {
if (croppedFile != null) {
Log.i("Camera", "result cancel. Hence, deleting file: " + croppedFile.getPath());
Log.i("File deleted ", croppedFile.delete() + "");
}
if (mediaFile != null) {
Log.i("Camera", "result cancel. Hence, deleting file: " + mediaFile.getPath());
Log.i("File deleted ", mediaFile.delete() + "");
}
}
}
}
Everything works perfect as expected below Android 6.0. But it doesn't work on Android 6.0 Marshmallow. In fact it doesn't even open the camera :(
I don't know whether I have to do something special for marshmallow. I am not getting any kind of error too, that I can post it here. Please help me out.
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…