因此,我有下面的代碼打開相機,捕獲圖像並將其保存在SDCard上。android:相機不打開棉花糖
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() + "");
}
}
}
}
一切工作完美,預計低於Android 6.0。但它不適用於Android 6.0棉花糖。事實上,它甚至不打開相機:(
我不知道我是否必須做一些特別的棉花糖,我沒有得到任何形式的錯誤,我可以在這裏發佈。我出去
感謝
我曾經在有棒棒糖的設備上工作,然後在運行棉花糖的兩臺設備上失敗。我在啓動時收到一條消息 - 看起來您的設備不支持攝像頭(或已鎖定)。我通過Camera.open()追蹤到Camera.cameraInitVersion()的錯誤,它返回了無法訪問Camera服務的錯誤。目前尚未意識到對棉花糖現在的許可模式需要進行編碼更改。 – medloh
@ChintanSoni是它顯示一個請求只有一次在應用程序? – RushDroid
什麼是mediaFile?它是一個文件?感謝這個例子! –