Android Question Keep JPEG Exif meta data

Michael2150

Member
Licensed User
Hi I have an image coming in from the camera using the MediaChooser.

I am trying to compress the image, but when I do it loses all the exif data. This messes up the rotation on some devices etc. I am using Smm to display the images, the images are displayed perfectly before trying to compress them.

What is the best solution for taking the original file returned by the camera, and without reading it into the app as a bitmap, compress it and save it as a new file?

The code I used is provided below:

B4X:
Sub Class_Globals
    Public Bitmap As B4XBitmap
    Private FileName As String = ""
    Private FilePath As String = ""
End Sub

Public Sub Initialize(info As MediaChooserResult)
    FileName = info.MediaFile
    FilePath = info.MediaDir
End Sub

Public Sub LoadImage As Boolean
    Try
        If File.Exists(FilePath,FileName) Then
            Bitmap = LoadBitmap(FilePath,FileName)
            Return True
        End If
    Catch
        Logger.LogException(LastException)
    End Try
    Return False
End Sub

Public Sub Compress
    Dim quality As Int = 80
    Dim originalSize As Double = Round2(File.Size(FilePath, FileName)/(1024*1024), 3)
    Log($"Starting Size: ${originalSize}MB"$)
    If LoadImage Then
        Dim Out As OutputStream = File.OpenOutput(FilePath, FileName, False)
        Bitmap.WriteToStream(Out, quality, "JPEG")
        Dim s As Double = Round2(File.Size(FilePath, FileName)/(1024*1024), 3)
        Log($"Saved image with quality ${quality} - ${s}MB ratio: ${originalSize/s}"$)
    Else
        Log("Could not load image")
    End If
End Sub
 
Last edited:
Top