因此,我有下面的代码打开相机,捕获图像并将其保存在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?它是一个文件?感谢这个例子! –