Android 相机 api 创建一个空文件
Android camera api creates a null file
你好,我正在尝试在我的应用程序中使用相机 api 来拍照,它也可以正常工作,拍照并保存,但后来我选择了相机并按下后退按钮,但没有拍照图片,它会创建一个 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;
}
it creates a file with 0kb
不,您 通过调用 createNewFile()
创建了一个 0KB 文件。
how do I fix this?
删除您对 createNewFile()
的通话。那是没有必要的。
你好,我正在尝试在我的应用程序中使用相机 api 来拍照,它也可以正常工作,拍照并保存,但后来我选择了相机并按下后退按钮,但没有拍照图片,它会创建一个 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;
}
it creates a file with 0kb
不,您 通过调用 createNewFile()
创建了一个 0KB 文件。
how do I fix this?
删除您对 createNewFile()
的通话。那是没有必要的。