I am trying to take image and save it in a folder using camera intent. The code is working very fine in other Android versions (upto Android 10). But the same code is not running on Android 11. I am also recording video using Custom video camera, but it is working fine in Android 11 also. Please help me with the same.
Camera code :-
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = Utilites.createImageFile(photocount, getActivity());
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(getActivity(), "Catched No Directory",
Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, CAMERA_CAPTURE);
} else {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, CAMERA_CAPTURE);
}
} else {
Toast.makeText(getActivity(), "No Directry", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), "Take pic intent null", Toast.LENGTH_SHORT).show();
}
The Utility class code where the image file is created. The folder name for saving images is based on the number generated from server.
public static File createImageFile(int id, Activity activity) throws IOException {
String mCurrentPhotoPath;
// Create an image file name
String imageFileName = "";
File storageDir = new File(activity.getFilesDir() + "/Photos/" + Job_Inspections_Activity.LeadID + "/Pictures/");
if (!storageDir.exists()) {
storageDir.mkdirs();
}
String directory = storageDir.toString();
for (int i = 1; i <= id; i++) {
Long tsLong = System.currentTimeMillis() / 1000;
String ts = tsLong.toString();
File file = new File(directory + "/" + ts + "-" + i + "~Photo.jpg");
if (file.exists()) {
// do nothing
// as the image already exists and if we do anything it might override it
//so leave this condition as it is.
} else {
// if the given file name is not found then pass the first name that is encountered and do not exists.
// and then break the loop as it useless to continue to do so.
imageFileName = ts + "-" + i + "~Photo.jpg";
break;
}
}
File image = new File(storageDir, imageFileName);
/*File.createTempFile(
imageFileName, *//* prefix *//*
".jpg", *//* suffix *//*
storageDir *//* directory *//*
);*/
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
I have checked in device explorer of the emulator, it is showing the files created, using above method, but camera always returns 0 (zero) in result code.
My module level build.gradle file info
compileSdkVersion 29
buildToolsVersion "28.0.3"
minSdkVersion 19
targetSdkVersion 29
I am also not getting any warning/error messages in Logcat.
I have tried many solutions for this problem, but none of them worked.