'Android - onActivityResult return NULL when I try to take picture
I implemented on my app a function that I use to take pictures, usually it works but sometimes it happens, however, that the images are not saved, I have tried to debug but I cannot understand why it is not working properly.
Main function used to create the path name, to set the orientation, and to set the destination folder
public void takePhoto(CoordsDamage coordsDamage) {
try {
int id = dbGest.getIdDamages(coordsDamage, context);
int nItem = new FilesUtilities().photoDamages(id, coordsDamage, context);
int maxPhoto = Integer.parseInt(dbGest.getSetting("maxPhoto", context));
String imageFileName = EXT_DAMAGE + coordsDamage.getWizard() + "_" + coordsDamage.getIncarico() + "_" + coordsDamage.getComponent() + "_an" + id + "_" + (nItem + 1) + ".jpg";
if (nItem < maxPhoto) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
File storageDir = new File(new GuiConst(context).getPdfFolderC(false));
if (!storageDir.exists()) {
new FilesUtilities().createFolder(context);
}
File photoFile = new File(storageDir, imageFileName);
photoInfo = coordsDamage;
imagePath = photoFile.getPath();
imageName = photoFile.getName();
dbGest.insertIntoFileData(wizardID, jobID, "damages", imagePath, imageName, context);
dbGest.updatePath(imagePath, photoInfo, context);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(context, "myprovider.provider", photoFile));
startActivityForResult(cameraIntent, 1);
}
} else {
Toast.makeText(ExternalDamagesNoTutorial.this, "Raggiunto limite massimo di foto per questa anomalia", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
wil.WriteFile("externalDamagesNoTutorial takePhoto - Exception: " + e.getMessage(), context);
}
}
onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
if (requestCode == 1 && resultCode == RESULT_OK) {
showDialogPhotos(photoInfo);
Toast.makeText(this, context.getResources().getString(R.string.label_check_photo_ok), Toast.LENGTH_SHORT).show();
} else if (requestCode == 1 && resultCode == RESULT_CANCELED) {
dbGest.deleteFileToSendRecord(imageName, context);
dbGest.removePath(imagePath, photoInfo, context);
Toast.makeText(this, context.getResources().getString(R.string.label_check_photo_ko), Toast.LENGTH_SHORT).show();
}
loadPin = false;
} catch (Exception e) {
wil.WriteFile("externalDamagesNoTutorial onActivityResult - Exception: " + e.getMessage(), context);
}
}
photoInfo,imageName,imageName they are global variables that contain some information I need in the onActivityResult, and when the image is not saved these values take null value.
The strange thing is that resultCode still takes a RESULT_OK value even when the photo is not saved.
To summarize: typically the application works sometimes it happens that the image is not saved on the filesystem
ideas?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
