[英]Picture File on RESULT_CANCELED
I want to be able to track image file names when a picture has been taken with the default Camera Glassware. 使用默认的相机玻璃器皿拍照后,我希望能够跟踪图像文件名。 This is so I can delete them when finished. 这样一来,我可以在完成后将其删除。 I have the following code: 我有以下代码:
...
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
String imgPath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
mService.addToImageQueue(imgPath);
}
else if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_CANCELED) {
...
}
}
If I tap, the resultCode returns RESULT_OK
. 如果我点击,则resultCode返回RESULT_OK
。 When I dismiss (swipe down), I get the resultCode RESULT_CANCELED
. 当我关闭(向下滑动)时,我得到了resultCode RESULT_CANCELED
。 This is how I intended it to work, except it still generates the image file even if the resultCode is RESULT_CANCELED
... I honestly feel like this might be a bug since I tried to use data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
这就是我想要的工作方式,除了即使resultCode为RESULT_CANCELED
仍会生成图像文件...老实说,这可能是一个错误,因为我尝试使用data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
and got a NullPointerException
. 并得到了NullPointerException
。 Am I doing something wrong? 难道我做错了什么? Is there a way to get this file name even on RESULT_CANCELED
? 有没有办法在RESULT_CANCELED
上获取此文件名?
You could create a temporary file first (look at the createImageFile()
method in this tutorial ). 您可以先创建一个临时文件(请参阅本教程中的createImageFile()
方法)。 If successfully created, do two things: 如果成功创建,请执行以下两项操作:
putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile))
). 将该文件的URI包含在额外的意图中( putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile))
)。 If resultCode is RESULT_CANCELED
, you can now trace back to the path of the temporary file and call delete()
on it. 如果resultCode为RESULT_CANCELED
,您现在可以追溯到临时文件的路径并对其调用delete()
。
Here is some sample code: 这是一些示例代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Log.v("MainActivity", "Result successful.");
} else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_CANCELED) {
Log.v(TAG, "Result canceled. Uri of file is " + mCurrentPhotoPath);
File file = new File(mCurrentPhotoPath);
if (file.exists()) {
Log.v(TAG, "File exists.");
if(file.delete()) {
Log.v(TAG, "File was successfully deleted!");
} else {
Log.v(TAG, "File not successfully deleted.");
}
} else {
Log.v(TAG, "File does not exist!");
}
}
}
Note: For new File(mCurrentPhotoPath)
to work, remove "file:" from the beginning of mCurrentPhotoPath
. 注意:为了使new File(mCurrentPhotoPath)
正常工作,请从mCurrentPhotoPath
的开头删除“ file:”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.