Mattia
Mattia

Reputation: 1117

Android 12 - CAMERA2 - Pictures are corrupted when saving

I found an anomaly during tests with this particular device, the details are below:

Hardware: samsung,SM-A326B (Smartphone Samsung A32)

OS: Android 12

ONE UI: 4.1

Google Play: 01/may/22

Patch level: 01/june/22

These are the specs of one device that works properly, the only differences that I see are related to different version of the patch level.

Hardware: samsung,SM-A515F (Smartphone Samsung A51)

OS: Android 12

ONE UI: 4.1

Google Play: 01/may/22

Patch level: 01/may/22

I have implemented a customization of camera2 in my application, this always works except with this device, the specific anomaly is that the images are saved completely white and are corrupted, the image can only be recovered by third party software.

These are the permissions I ask for in the manifest:

<uses-feature android:name="android.hardware.location.gps " />
<uses-feature android:name="android.hardware.location.network" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

This is the code that I use to save the images

public void saveCameraImage(Image mImage, File mFile, String mOrientation, Context context) {
    ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
    byte[] bytes = new byte[buffer.remaining()];
    buffer.get(bytes);
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(mFile);
        fileOutputStream.write(bytes);
    } catch (IOException e) {
        wil.WriteFile("saveImage - Exception: " + e.getMessage(), context);
    } finally {
        mImage.close();
        if (null != fileOutputStream) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                wil.WriteFile("saveImage - Exception: " + e.getMessage(), context);
            } finally {
                saveExifData(mFile.getPath(), context);
                createThumbs(mOrientation, mFile, context);
            }
        }
    }
}

This is the function that I named "saveExifData"

private void saveExifData(String filepath, Context context) {
        try {
            if (filepath.endsWith(IMAGE_FORMAT)) {
                File file = new File(filepath);
                if (file.exists()) {
                    DbGest dbGest = DbGest.getInstance(context);
                    String dateTime = dbGest.getTimestampFromFileToSend(filepath, "dateTime", context);
                    String dateTimeGps = dbGest.getTimestampFromFileToSend(filepath, "gps", context);
                    String dateTimeDesc = dbGest.getTimestampFromFileToSend(filepath, "dateTimeDesc", context);
    
                    ExifInterface exifInterface = new ExifInterface(filepath);
                    exifInterface.setAttribute(ExifInterface.TAG_COPYRIGHT, "xxxxx ");
                    exifInterface.setAttribute(ExifInterface.TAG_MODEL, Build.MANUFACTURER + "," + Build.MODEL);
                    exifInterface.setAttribute(ExifInterface.TAG_SOFTWARE, dbGest.getSetting("appName", context));
                    exifInterface.setAttribute(ExifInterface.TAG_DATETIME, dateTime);
                    exifInterface.setAttribute(ExifInterface.TAG_DATETIME_ORIGINAL, dateTime);
                    exifInterface.setAttribute(ExifInterface.TAG_USER_COMMENT, "Immagine scattata tramite " + dbGest.getSetting("appName", context) + " il " + dateTimeDesc);
    
                    CoordinatesData position = DbGest.getInstance(context).getBetterPositionKnown(context);
                    exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE, String.valueOf(position.getLat()));
                    exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, String.valueOf(position.getLng()));
                    exifInterface.setAttribute(ExifInterface.TAG_GPS_DATESTAMP, dateTimeGps);
                    exifInterface.setAttribute(ExifInterface.TAG_GPS_SPEED, String.valueOf(position.getSpeed()));
                    exifInterface.setLatLong(position.getLat(), position.getLng());
                    exifInterface.saveAttributes();
                }
            }
        } catch (Exception e) {
            wil.WriteFile("saveExifData - Exception: " + e.getMessage(), context);
        }
    }

So I guess it's not a permission issue, also there are no exceptions, so I don't know how to fix this

Upvotes: 1

Views: 318

Answers (1)

Mattia
Mattia

Reputation: 1117

After several test and debugging I find the cause of the bug.

The problem it is in the "saveExifData", for some reason this row of code

exifInterface.setAttribute(ExifInterface.TAG_COPYRIGHT, "xxxx");

it is the cause of the problem

Upvotes: 0

Related Questions