Android摄像头api创建一个空文件
您好,我正在尝试在我的应用程序中使用camera api来拍照,它也可以正常工作,可以拍照并保存,但是我选择camera并按下back按钮,在不拍照的情况下,它会创建一个0 KB的文件,如何修复Android摄像头api创建一个空文件,android,android-camera,Android,Android Camera,您好,我正在尝试在我的应用程序中使用camera api来拍照,它也可以正常工作,可以拍照并保存,但是我选择camera并按下back按钮,在不拍照的情况下,它会创建一个0 KB的文件,如何修复 private static final int CAMERA_REQUEST_CODE = 100; private static final int RESULT_LOAD_IMAGE = 101; private void ShowDialogForChoose() { final Al
private static final int CAMERA_REQUEST_CODE = 100;
private static final int RESULT_LOAD_IMAGE = 101;
private void ShowDialogForChoose() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, new String[] {
"Camera", "Gallery" }),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(
MediaStore.EXTRA_OUTPUT,
getTempUri());
// Lav check om der er primission til at bruge camera
startActivityForResult(cameraIntent,
CAMERA_REQUEST_CODE);
} else if (which == 1) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
}
});
builder.create().show();
}
private Uri getTempUri() {
// Create an image file name
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String dt = sdf.format(new Date());
imageFile = null;
imageFile = new File(Environment.getExternalStorageDirectory()
+ "/Fitness/", "Camera_" + dt + ".jpg");
AppLog.Log(
TAG,
"New Camera Image Path:- "
+ Environment.getExternalStorageDirectory()
+ "/Fitness/" + "Camera_" + dt + ".jpg");
File file = new File(Environment.getExternalStorageDirectory()
+ "/Fitness");
if (!file.exists()) {
file.mkdir();
}
if (!imageFile.exists()) {
try {
imageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
imagePath = Environment.getExternalStorageDirectory() + "/Fitness/"
+ "Camera_" + dt + ".jpg";
imageUri = Uri.fromFile(imageFile);
return imageUri;
}
它创建一个0 KB的文件
不,您可以通过调用createNewFile创建一个0KB的文件
我该如何解决这个问题
private static final int CAMERA_REQUEST_CODE = 100;
private static final int RESULT_LOAD_IMAGE = 101;
private void ShowDialogForChoose() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, new String[] {
"Camera", "Gallery" }),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(
MediaStore.EXTRA_OUTPUT,
getTempUri());
// Lav check om der er primission til at bruge camera
startActivityForResult(cameraIntent,
CAMERA_REQUEST_CODE);
} else if (which == 1) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
}
});
builder.create().show();
}
private Uri getTempUri() {
// Create an image file name
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String dt = sdf.format(new Date());
imageFile = null;
imageFile = new File(Environment.getExternalStorageDirectory()
+ "/Fitness/", "Camera_" + dt + ".jpg");
AppLog.Log(
TAG,
"New Camera Image Path:- "
+ Environment.getExternalStorageDirectory()
+ "/Fitness/" + "Camera_" + dt + ".jpg");
File file = new File(Environment.getExternalStorageDirectory()
+ "/Fitness");
if (!file.exists()) {
file.mkdir();
}
if (!imageFile.exists()) {
try {
imageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
imagePath = Environment.getExternalStorageDirectory() + "/Fitness/"
+ "Camera_" + dt + ".jpg";
imageUri = Uri.fromFile(imageFile);
return imageUri;
}
删除对createNewFile的调用。这是没有必要的