gpt4 book ai didi

java - 相机应用程序在按下后退按钮时保存空文件

转载 作者:行者123 更新时间:2023-11-30 00:21:53 25 4
gpt4 key购买 nike

我在 fragment 中使用相机应用程序。一切正常,拍摄的照片保存在我希望的位置。

问题是,当我在照片应用程序中按下设备上的后退按钮时,会保存一张空图像。我怎样才能避免这种情况?

这是我的 CameraActivity:

public class CameraActivity extends Activity {
private static final String TAG = "CameraActivity";
String currentFilePath;
String currentFileName;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

new CameraTask().execute();
}

public void dispatchPictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
Toast.makeText(this, R.string.msg_picture_not_saved, Toast.LENGTH_LONG).show();
}
if (photoFile != null) {
Uri photoUri = FileProvider.getUriForFile(this, getClass().getCanonicalName(), photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent, Constants.REQUEST_TO_TAKE_PICTURE);

addPictureToGallery();
dispatchDataIntent();
}
}
}

public File createImageFile() throws IOException {
File pathOfStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/Camera");
pathOfStorageDir.mkdir();

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String filePrefix = "img_" + timeStamp + "_";
String suffix = ".jpg";

File image = File.createTempFile(filePrefix, suffix, pathOfStorageDir);
currentFileName = image.getName();
currentFilePath = image.getAbsolutePath();
return image;
}

private void addPictureToGallery() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentFilePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}

private void dispatchDataIntent() {
Intent data = new Intent();
data.putExtra(Constants.IMAGE_FILE_NAME, currentFileName);
setResult(CameraActivity.RESULT_OK, data);
}

private class CameraTask extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {
dispatchPictureIntent();
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
finish();
}
}
}

感谢您的帮助!

最佳答案

您的代码总是保存一个图像文件。如果用户按下后退按钮,尝试删除保存的图片。比较好的方法是在onActivityResult中保存图片,检查结果码后。

public class CameraActivity extends Activity {
private static final String TAG = "CameraActivity";
String currentFilePath;
String currentFileName;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

new CameraTask().execute();
}

public void dispatchPictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
Toast.makeText(this, R.string.msg_picture_not_saved, Toast.LENGTH_LONG).show();
}
if (photoFile != null) {
Uri photoUri = FileProvider.getUriForFile(this, getClass().getCanonicalName(), photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent, Constants.REQUEST_TO_TAKE_PICTURE);

}
}
}

public File createImageFile() throws IOException {
File pathOfStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/Camera");
pathOfStorageDir.mkdir();

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String filePrefix = "img_" + timeStamp + "_";
String suffix = ".jpg";

File image = File.createTempFile(filePrefix, suffix, pathOfStorageDir);
currentFileName = image.getName();
currentFilePath = image.getAbsolutePath();
return image;
}

private void addPictureToGallery() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentFilePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}

private void dispatchDataIntent() {
Intent data = new Intent();
data.putExtra(Constants.IMAGE_FILE_NAME, currentFileName);
setResult(CameraActivity.RESULT_OK, data);
}

private class CameraTask extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {
dispatchPictureIntent();
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_TO_TAKE_PICTURE){
if(resultCode == RESULT_OK) {

addPictureToGallery();
dispatchDataIntent();
} else {
File f = new File(currentFilePath);
f.delete();
}
}
finish();

}

关于java - 相机应用程序在按下后退按钮时保存空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46139600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com