I just need to add some Exif metadata to a Jpeg saved to MediaStore (using Content Resolver)
Here how I save the image:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATE_ADDED, now);
values.put(MediaStore.MediaColumns.DATE_MODIFIED, now);
ContentResolver resolver = context.getContentResolver();
Uri uri = resolver.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values );
try( OutputStream stream = resolver.openOutputStream(uri) )
{
// Perform operations on "stream".
bitmap.compress( Bitmap.CompressFormat.JPEG, 90, stream );
stream.flush();
} catch( IOException e )
{
e.printStackTrace();
}
Here how I try to add Exif metadata:
private void writeExif( Uri uri, List<String> exifStr )
{
try
{
// THIS fails on file opening ( Read Failed: EBADF (Bad File Descriptor)
// uri = MediaStore.setRequireOriginal(uri); // UNUSEFULL
ParcelFileDescriptor imageFd = getContentResolver().openFileDescriptor(uri, "w");
ExifInterface exif = new ExifInterface( imageFd.getFileDescriptor() );
// THIS fails on file opening ( open failed: EACCES (Permission denied) )
// String filename = getRealPathFromURI( uri ); // Extract full pathname from (deprecataed) MediaStore.Images.Media.DATA column
// ExifInterface exif = new ExifInterface( filename );
// THIS fails on saveAttributes ( write failed: EBADF (Bad file descriptor) )
// ExifInterface exif = new ExifInterface( getContentResolver().openInputStream(uri) );
for( int i = 0; i < ExifAttributes.length; i++ )
{
String value = exifStr.get(i);
if( value != null )
exif.setAttribute(ExifAttributes[i], value);
}
exif.saveAttributes();
}
catch( Exception e )
{
e.printStackTrace();
}
}
In any way, I try to do it (check commented lines) I get some error (Bad File Descriptor or access denied)
The use of the path from 'MediaStore.Images.Media.DATA' is just a test and MUST not be used in the solution (I'm targeting API 29 and it is deprecated). But anyway, it does not work either.
Have anyone idea of how I can get rid of this?
Thanks
P.S. The image is correctly saved, just the writeExif() part fails.
I solved. The problem was that the file was not syncronized on disk.
So, here how it works for saving the image:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATE_ADDED, now);
values.put(MediaStore.MediaColumns.DATE_MODIFIED, now);
ContentResolver resolver = context.getContentResolver();
Uri uri = resolver.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values );
try( ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri,"w") )
{
FileDescriptor fd = pfd.getFileDescriptor();
try( OutputStream stream = new FileOutputStream(fd) )
{
// Perform operations on "stream".
mBitmap.compress( Bitmap.CompressFormat.JPEG, 90, stream );
}
// Synch data with disk. It's mandatory to be able later to call writeExif
fd.sync(); // <---- HERE THE SOLUTION
} catch( IOException e )
{
e.printStackTrace();
}
And how I add Exif metadata:
private void writeExif( Uri uri, List<String> exifStr )
{
try( ParcelFileDescriptor imagePfd = getContentResolver().openFileDescriptor(uri, "rw") )
{
ExifInterface exif = new ExifInterface( imagePfd.getFileDescriptor() );
for( int i = 0; i < ExifAttributes.length; i++ )
{
String value = exifStr.get(i);
if( value != null )
exif.setAttribute(ExifAttributes[i], value);
}
exif.saveAttributes();
}
catch( Exception e )
{
e.printStackTrace();
}
}