Since no one came with an answer but thanks to @blackapps comment I was able to find out the solution myself. I will post it here if someone else gets to this post searching for the answer for this same question.
It turns out that the ExifInterface
has a constructor that receives a FileDescriptor
as a parameter and it is possible to open a FileDescriptor
from an Uri
by using the ContentResolver
. Then the solution is easy and the only extra care one needs in order to successfully open the FileDescriptor
is to use the "rw" mode since the ExifInterface
will need to read and write info and not just only write.
Here goes a sample code:
InputStream ins = null;
ParcelFileDescriptor outFd = null;
try {
ins = context.getContentResolver().openInputStream(imageInputUri);
ExifInterface originalExif = new ExifInterface(ins);
outFd = context.getContentResolver().openFileDescriptor(imageOutputUri, "rw");
ExifInterface newExif = new ExifInterface(outFd.getFileDescriptor());
copyExifAttributes(originalExif, newExif, width, height);
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
} finally {
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
}
}
if (outFd != null) {
try {
outFd.close();
} catch (IOException e) {
Log.d(TAG, e.getMessage(), e);
}
}
}
Be warned though that this code wont work on Android versions older than Lollipop (api 21). That's an ExifInterface
limitation since it needs a seekable FileDescriptor in order to work and the ExifiInterface
uses a native call for doing this. Looking at the Android docs is possible to see that this native code is only available since Lollipop.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…