Skip to content

saveImageTempSync ​

Synchronously Save Image to a Temporary File ​

This method synchronously saves the image pixel data passed in by the SDK as a local temporary image file on the App. It is typically used for game screenshots, share images, QR code images, record images, and other scenarios that require quickly generating a local image path.

Unlike saveImageTemp, saveImageTempSync does not return the result asynchronously via SUDOPStateHandle, but returns the temporary file path directly through the method's return value.


Method Declaration ​

java
/**
 * Synchronously save an image to a temporary file
 *
 * @param params Request parameters
 * @return Returns the temporary image file path on success; returns null on failure
 */
String saveImageTempSync(SUDOPSaveImageTempParams params);

Parameters ​

SUDOPSaveImageTempParams ​

java
public class SUDOPSaveImageTempParams {
    /**
     * Pixel data
     */
    public byte[] data;

    /**
     * Width of the image to write, maximum width is 4096
     */
    public int width;

    /**
     * Height of the image to write, maximum height is 4096
     */
    public int height;

    /**
     * Format of the image to write, supported types are jpg, png
     */
    public String fileType;

    /**
     * Whether the written data needs to be flipped along the y-axis, default is false
     */
    public boolean reverse;
}
FieldTypeDescription
databyte[]Image pixel data, usually RGBA pixel data
widthintWidth of the image to write, maximum width is 4096
heightintHeight of the image to write, maximum height is 4096
fileTypeStringFormat of the image to write; currently supports jpg, png
reversebooleanWhether the written data needs to be flipped along the Y-axis, default is false

Return Value ​

This method returns the save result directly through the return value.

Save Success ​

Returns the temporary image file path:

java
return filePath;

Example:

text
/data/user/0/com.xxx.xxx/cache/sud_temp_123456.png

Save Failure ​

Returns null:

java
return null;

Common failure scenarios include:

  • fileType is not jpg or png
  • Image pixel data is empty or abnormal
  • Image width or height is invalid
  • File creation failed
  • Image encoding failed
  • An IOException occurred during file writing

Runtime Behavior ​

  • Synchronous execution: After this method is called, image encoding and file writing complete on the current thread.
  • The App handles file generation: The App side needs to create a temporary image file based on params.data, params.width, params.height, and params.fileType.
  • Direct result return: Returns the temporary file path on success, and null on failure.
  • No asynchronous callback: This method does not use SUDOPStateHandle, and does not need to call handle.success or handle.failure.
  • Temporary file lifecycle: The returned file path is a temporary file; the App can clean it up at an appropriate time according to its own caching strategy.

Example Code ​

java
@Override
public String saveImageTempSync(SUDOPSaveImageTempParams params) {
    String filePath = getTempFile(params.fileType);

    try {
        if ("jpg".equals(params.fileType)) {
            QgClientUtils.rgbaToJpeg(
                    params.data,
                    params.width,
                    params.height,
                    filePath
            );
        } else if ("png".equals(params.fileType)) {
            QgClientUtils.rgbaToPng(
                    params.data,
                    params.width,
                    params.height,
                    filePath
            );
        } else {
            return null;
        }
    } catch (IOException e) {
        return null;
    }

    return filePath;
}

text
SDK calls saveImageTempSync
        ↓
App validates the params parameters
        ↓
Create the temporary file path
        ↓
Determine fileType
        ↓
jpg: encode the RGBA data as a JPEG file
png: encode the RGBA data as a PNG file
other formats: return null
        ↓
Write success: return tempFilePath
Write failure: return null

Notes ​

  • Synchronous time cost: This method executes image encoding and disk writing on the current thread, which may be time-consuming. If called on the main thread, it may cause stuttering.
  • Control image size: It is recommended that width and height do not exceed 4096, to avoid excessive memory usage or encoding failure.
  • Only specific formats supported: Currently only jpg and png are supported; unsupported formats should return null.
  • Return null on failure: This interface has no error code or error message; on save failure it uniformly returns null.
  • Data integrity check: Before saving, it is recommended to check whether params.data is empty and whether the data length matches the image width and height.
  • Y-axis flipping: If params.reverse is true, the App side should flip the pixel data along the Y-axis before writing the image.
  • File path validity: The returned path should be a local temporary file path readable by the App.
  • Temporary file cleanup: The App should clean up temporary files as needed to avoid the cache taking up too much storage space.
  • Permission recommendation: It is recommended to save the image to the App's private cache directory, to avoid requesting additional external storage permissions.