当用户点击叉号不接受照片时,它会以与接受所拍摄照片相同的方式结束意图。它会将文件保存到设备画廊,但是文件为空。点击叉号难道不意味着resultCode != RESULT_OK吗?我是否遗漏了其他检查?谢谢。以下是代码。等等,我在活动结果之前保存了图像...这是一个有缺陷的系统,但它出现在官方Android开发者网站上。如果有人能提供修复建议,我会非常感激,因为我曾经在onActivtyResult中保存图像,但在某些手机上无法工作,导致异常,所以我改变了这个方法。
开始意图:
开始意图:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
ih.galleryAddPic(mCurrentPhotoPath, this.getApplicationContext());
return image;
}
在onActivityResult中的相机意图案例:
else if ((requestCode == REQUEST_TAKE_PHOTO) && (resultcode == RESULT_OK)){
mProfilePicPath = mCurrentPhotoPath;
mPortraitPhoto = ih.decodeSampledBitmapFromImagePath(mCurrentPhotoPath,
GlobalConstants.PROFILE_PICTURE_RESOLUTION,
GlobalConstants.PROFILE_PICTURE_RESOLUTION);
TextView tv = (TextView) findViewById(id.ProfilePicText);
tv.setText(mProfilePicPath);
}
}catch(Exception ex){
Log.d("shkdghrfb", ex.toString());
}
}
编辑:我将onActivityResult更改为以下内容,但结果没有改变(空白图像仍然存在于我的相册中,deleted的值为true):
else if (requestCode == REQUEST_TAKE_PHOTO){
if(resultcode == RESULT_OK){
File f = new File(mCurrentPhotoPath);
mProfilePicPath = null;
if (f.exists()) {
if (f.length() != 0){
mProfilePicPath = mCurrentPhotoPath;
mPortraitPhoto = ih.decodeSampledBitmapFromImagePath(mCurrentPhotoPath,
GlobalConstants.PROFILE_PICTURE_RESOLUTION,
GlobalConstants.PROFILE_PICTURE_RESOLUTION);
TextView tv = (TextView) findViewById(id.ProfilePicText);
tv.setText(mProfilePicPath);
}
else {
boolean deleted = f.delete();
if (deleted == true){
Log.d("camera0", "deleted");
}
else{
Log.d("camera0", "not deleted");
}
}
}
}
else{
File f = new File(mCurrentPhotoPath);
boolean deleted = f.delete();
if (deleted == true){
Log.d("camera", "deleted");
}
else{
Log.d("camera", "not deleted");
}
}
}
}catch(Exception ex){
Log.d("shkdghrfb", ex.toString());
}
}catch(Exception ex){
Log.d("shkdghrfb", ex.toString());
}
编辑 好的,我相信在删除后需要使用MediaScannerIntent扫描SD卡的适当区域,以便它显示出来,因为现在它似乎已经可以工作了。
createImageFile()
中,mCurrentPhotoPath
被设置为file.getAbsolutePath
。谢谢。 - user3164083createImageFile()
中不创建实际的文件(但仍然创建一个带有效路径的有效文件对象)。 - Sam