There is no OK button displayed when I capture an image from the camera. If I press the back button, The control goes to the onActivityResult but the resultCode is not OK.
The camera opens fine and I can see the files being created in the folder that I specified, but their size is 0, maybe because the app has not saved the captured image yet.
BTW, the device's Android version is Pie. The app works on older versions (Kitkat and Marshmallow). Haven't tried it on Nougat and Oreo and went straight to Pie.
Here is my code:
public void launchCamera() {
String[] permissions = new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};
boolean hasPermission = (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
//ask permission
requestPermissions(permissions, REQUEST_WRITE_STORAGE);
}
else {
hasPermission = (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
requestPermissions(permissions, REQUEST_WRITE_STORAGE);
}
else {
hasPermission = (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
requestPermissions(permissions, REQUEST_WRITE_STORAGE);
}
else {
cameraUsePermitted();
}
}
}
}
The onRequestPermission handler
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
launchCamera();
}
else {
Utils.toast(getApplicationContext(),
"App must be granted permission to use the camera.");
}
break;
}
}
This launches the camera
private void cameraUsePermitted() {
// the intent is my camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//getting uri of the file
if (Build.VERSION.SDK_INT < 24) {
filenameOfCapturedImage = Uri.fromFile(getFile());
Utils.log("FILENAME", "1");
}
else {
filenameOfCapturedImage = Uri.parse(getFile().getPath());
Utils.log("FILENAME", "2");
}
Utils.log("FILENAME", filenameOfCapturedImage.getPath());
//Setting the file Uri to my photo
intent.putExtra(MediaStore.EXTRA_OUTPUT, filenameOfCapturedImage);
if(intent.resolveActivity(getPackageManager())!=null)
{
startActivityForResult(intent, PICTURE_RESULT);
}
}
And the onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICTURE_RESULT:
Utils.log("PICTURE", "1");
if(resultCode == RESULT_OK) {
Utils.log("PICTURE", "2");
ReturnHeaderDisplay header = getCurrentHeader();
String filename= filenameOfCapturedImage.getPath();
long newId;
newId = ReturnImages.addImage(getApplicationContext(),
header.getReturnId(),
filename);
if (newId >= 0) {
ReturnImages.saveImage(getApplicationContext(), (int)newId, filename, header.getReturnId(), header.getCustomerName());
returnImages.add(ReturnImages.loadReturnImageById(getApplicationContext(), (int)newId));
addToReceipt(returnImages.get(returnImages.size()-1));
}
else {
AppMessages.showMessage(ReturnActivity.this,
"Unable to save image to database.",
"Image Not Saved",
AppMessages.MESSAGE_INFO,
null);
}
}
else {
Utils.log("PICTURE", "3");
}
break;
}
}
the getFile() method
private File getFile() {
File folder = Environment.getExternalStoragePublicDirectory(SD_CARD_IMAGE_PATH);
//if it doesn't exist the folder will be created
if (!folder.exists()) {
folder.mkdirs();
}
String imageFileName = "RETURN_" + UUID.randomUUID();
File image_file = null;
try {
image_file = File.createTempFile(imageFileName, ".jpg", folder);
} catch (IOException e) {
e.printStackTrace();
}
return image_file;
}