I am working on app for Android versions 26 through 29. The code for 29, using mediaStore.insert is working fine and I won't show it here.
The problem occurs when I branch in code for < Q. The app takes photos and uploads them via a web api. The photos are also written to the DCIM directory and can be later selected and also uploaded.
Here is the code I am using for <Q to save the image in to DCIM
String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
File image = new File(imagesDir, imageFileNameToUseAtWebServerEnd);
try
{
OutputStream fos = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Objects.requireNonNull(fos).close();
} catch (IOException e)
{
Log.d(TAG, e.toString());
}
and here is the code to select from DCIM (or any gallery) and generate a file object
if (originalUri != null)
{
Log.d(TAG, "At top of handleChosenPhotoV2 - about to setPicV2");
ContentResolver resolver = getContentResolver();
Cursor returnCursor =
resolver.query(originalUri, null, null, null, null);
assert returnCursor != null;
returnCursor.moveToFirst();
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
String name = returnCursor.getString(nameIndex);
nameIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
int intSize = returnCursor.getInt(nameIndex);
String dateStamp = new SimpleDateFormat("yyyyMMdd").format(new Date());
String timeStamp = new SimpleDateFormat("HHmmSS").format(new Date());
timeStamp = timeStamp.substring(0, 6);
imageFileNameToUseAtWebServerEnd = strTrial + "_" + dateStamp + "_" + timeStamp + "_" + strUserId + ".jpg";
try {
InputStream stream = resolver.openInputStream(originalUri);
File[] storageDir = getExternalMediaDirs();
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir[0] /* directory */
);
OutputStream outputStream = new FileOutputStream(image);
copyStream(stream, outputStream);
stream.close();
outputStream.close();
mCurrentPhotoPath = image.getAbsolutePath();
f = new File(mCurrentPhotoPath);
} catch (IOException e) {
Log.d(TAG, e.toString());
}
}
I then take the retrieved jpg file and apply code to correct the rotation but it does not work. (the same code works fine on the photos when they first come in from the camera)
I can only assume that somewhere along the line I have lost the ExifInterface information.
Many thanks in advance for any suggestions Tony