Skip to content

saveImageTemp ​

Save Image to a Temporary File ​

This method 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 similar scenarios.

The App side needs to generate a temporary image file based on the pixel data, image width/height, image format, and other parameters, and return the temporary file path to the SDK via handle.success(resultJson).


Method Declaration ​

java
/**
 * Save an image to a temporary file
 *
 * @param handle The state handle used to return the processing result to the SDK.
 *               Data example on success: {"tempFilePath":"filePath","errMsg":"msg"}
 * @param params Request parameters
 */
void saveImageTemp(SUDOPStateHandle handle, 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
widthintImage width, maximum width is 4096
heightintImage height, 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 ​

Return the image save result to the SDK via SUDOPStateHandle.

Save Success ​

On save success, call:

java
handle.success(dataJson);

Here dataJson is a JSON string, for example:

json
{
  "tempFilePath": "/data/user/0/xxx/cache/sud_temp_xxx.png"
}
FieldTypeDescription
tempFilePathStringThe temporary image file path after a successful save
errMsgStringOptional field, error or hint message. Usually omitted on success

Save Failure ​

On save failure, call:

java
handle.failure(retCode, retMsg);
ParameterTypeDescription
retCodeintError code; 0 indicates success, non-0 indicates failure
retMsgStringError description

Example:

java
handle.failure(-1, "This file type is not supported: webp");

Runtime Behavior ​

  • Triggered by the SDK: When the SDK needs to save image pixel data as a temporary file, it calls saveImageTemp.
  • The App handles file generation: The App side needs to generate a local temporary image file based on params.data, params.width, params.height, and params.fileType.
  • Supports asynchronous processing: Image encoding and file writing may be time-consuming, so it is recommended to handle them on a worker thread to avoid blocking the main thread.
  • Result callback: After saving completes, return the temporary file path via handle.success(...); if saving fails, return the error message via handle.failure(...).
  • Temporary file lifecycle: This interface saves a temporary image file; the App can clean it up at an appropriate time according to its own caching strategy.

Example Code ​

java
@Override
public void saveImageTemp(SUDOPStateHandle handle, SUDOPSaveImageTempParams params) {
    new Thread() {
        @Override
        public void run() {
            super.run();

            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 {
                    handle.failure(
                            -1,
                            "This file type is not supported: " + params.fileType
                    );
                    return;
                }
            } catch (IOException e) {
                handle.failure(-1, "error:" + e);
                return;
            }

            JSONObject obj = new JSONObject();
            try {
                obj.put("tempFilePath", filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }

            String result = obj.toString();
            handle.success(result);
        }
    }.start();
}

text
SDK calls saveImageTemp
        ↓
App validates parameters data / width / height / fileType
        ↓
App creates the temporary file path
        ↓
Encode the RGBA pixel data as jpg or png based on fileType
        ↓
Write to the local temporary file
        ↓
Write success: handle.success({"tempFilePath":"..."})
Write failure: handle.failure(retCode, retMsg)

Notes ​

  • Callback required: Whether saving succeeds or fails, you must call handle.success(...) or handle.failure(...).
  • Asynchronous processing recommended: Image encoding and disk writing may be time-consuming, so it is recommended to execute them on a worker thread to avoid blocking the UI thread.
  • File format restriction: Currently only jpg and png are supported; unsupported formats must return failure.
  • Image size limit: width has a maximum of 4096 and height has a maximum of 4096; it is recommended to return failure when the limit is exceeded.
  • Data integrity: Before saving, it is recommended to check whether params.data is empty and whether the data length matches the pixel format corresponding to width * 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.
  • Temporary file cleanup: The returned tempFilePath is a temporary file path; the App can clean it up after sharing completes, uploading completes, or the cache expires.
  • Permission handling: It is recommended to save to the App's private cache directory first, to avoid requesting additional external storage permissions.