0
嗨我想在我的應用程序中使用相機API,使用它拍照,它也可以正常拍攝並保存它,但然後我選擇相機並按下後退按鈕,沒有拍照,它創建一個0kb的文件,我該如何解決這個問題?安卓相機API創建一個空文件
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;
}
謝謝:) :) – JonathanNet