OutOfMemoryError Occurs When Converting Slides to Images in Java

【版本】aspose-slides-25.3-android.via.java.jar
【使用代码如下】

val presentation = Presentation(fileTest)
totalCount = presentation.slides.size()
val size = Size(requireWidth, requireHeight)
val parseProgressParams = IParseCallback.ParseProgressParams()
for (slideIndex in 0 until totalCount) {
    val slide = presentation.slides.get_Item(slideIndex)
    val bitmap = slide.getImage(size)
    parseProgressParams.slideIndex = slideIndex
    parseProgressParams.totalCount = totalCount
    val  outputStream = ByteArrayOutputStream()
    bitmap?.save(outputStream,5)
//                    parseProgressParams.bitmap = convertToBitmap(outputStream)
    parseProgressParams.bitmapOutputStream = outputStream
    callback?.onParseProgress(parseProgressParams)
}

额外尝试:
情况1:当fileTest是:整机命名和升级分享 (1).pptx - Google Slides
当requireWidth=1536 requireHeight=864 程序无异常
当 requireWidth=2688 requireHeight=1512 不管任何机器 解析第4页时,得到以下错误。

java.lang.OutOfMemoryError: Failed to allocate a 33554448 byte allocation with 18742992 free bytes and 17MB until OOM, target footprint 268435456, growth limit 268435456
at com.aspose.slides.internal.wt.lj.setCapacity(Unknown Source:24)
at com.aspose.slides.internal.wt.lj.b(Unknown Source:8)
at com.aspose.slides.internal.wt.lj.write(Unknown Source:25)
at com.aspose.slides.internal.k2.mo.write(Unknown Source:2)
at android.graphics.Bitmap.nativeCompress(Native Method)
at android.graphics.Bitmap.compress(Bitmap.java:1539)
at com.aspose.slides.internal.v7.x4.x4(Unknown Source:81)
at com.aspose.slides.internal.b7.x4.x4(Unknown Source:20)
at com.aspose.slides.internal.b7.x4.rf(Unknown Source:8)
at com.aspose.slides.internal.b7.x4.rf(Unknown Source:54)
at com.aspose.slides.internal.b7.x4.x4(Unknown Source:69)
at com.aspose.slides.internal.uv.x4.x4(Unknown Source:62)
at com.aspose.slides.internal.uv.vj.x4(Unknown Source:25)
at com.aspose.slides.pmj.x4(Unknown Source:1570)
at com.aspose.slides.Picture.x4(Unknown Source:14)
at com.aspose.slides.slg.(Unknown Source:540)
at com.aspose.slides.slg.(Unknown Source:0)
at com.aspose.slides.wh.x4(Unknown Source:737)
at com.aspose.slides.wh.x4(Unknown Source:470)
at com.aspose.slides.vvy.x4(Unknown Source:197)
at com.aspose.slides.vvy.x4(Unknown Source:484)
at com.aspose.slides.q6n.x4(Unknown Source:4)
at com.aspose.slides.ot.mo(Unknown Source:156)
at com.aspose.slides.ot.x4(Unknown Source:14)
at com.aspose.slides.dxm.mo(Unknown Source:10)
at com.aspose.slides.dxm.x4(Unknown Source:262)
at com.aspose.slides.dxm.x4(Unknown Source:132)
at com.aspose.slides.dxm.x4(Unknown Source:37)
at com.aspose.slides.Slide.x4(Unknown Source:8)
at com.aspose.slides.Slide.rf(Unknown Source:6)
at com.aspose.slides.Slide.getImage(Unknown Source:4)

情况2:当fileTest是:【培训课件】背光模组与液晶基础知识.pptx - Google Slides
不管:requireWidth=1536 requireHeight=864 还是requireWidth=2688 requireHeight=1512
解析第14页时,得到以下错误。这个错误目前只有在一款机型上出现。

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 3995456 byte allocation with 3055312 free bytes and 2983KB until OOM, target footprint 268435456, growth limit 268435456
at com.aspose.slides.internal.wt.lj.toArray(Unknown Source:5)
at com.aspose.slides.pmj.x4(Unknown Source:1573)
at com.aspose.slides.Picture.x4(Unknown Source:14)
at com.aspose.slides.slg.(Unknown Source:540)
at com.aspose.slides.slg.(Unknown Source:0)
at com.aspose.slides.wh.x4(Unknown Source:737)
at com.aspose.slides.wh.x4(Unknown Source:470)
at com.aspose.slides.vvy.x4(Unknown Source:197)
at com.aspose.slides.vvy.x4(Unknown Source:484)
at com.aspose.slides.q6n.x4(Unknown Source:4)
at com.aspose.slides.ot.mo(Unknown Source:156)
at com.aspose.slides.ot.x4(Unknown Source:14)
at com.aspose.slides.dxm.mo(Unknown Source:10)
at com.aspose.slides.dxm.x4(Unknown Source:262)
at com.aspose.slides.dxm.x4(Unknown Source:132)
at com.aspose.slides.dxm.x4(Unknown Source:37)
at com.aspose.slides.Slide.x4(Unknown Source:8)
at com.aspose.slides.Slide.rf(Unknown Source:6)
at com.aspose.slides.Slide.getImage(Unknown Source:4)

@zhanghanjiang

It seems you are encountering an OutOfMemoryError when trying to convert certain slides from a PowerPoint presentation to images using Aspose.Slides. This issue typically arises when the requested image dimensions are too large for the available memory, especially on devices with limited resources.

Here are a few suggestions to help you resolve this issue:

  1. Reduce Image Size: Since you mentioned that the error occurs when using larger dimensions (e.g., requireWidth=2688 and requireHeight=1512), try reducing the dimensions to see if that resolves the issue. For example, using requireWidth=1536 and requireHeight=864 seems to work without errors.

  2. Optimize Memory Usage: Ensure that your application is optimized for memory usage. You can try to:

    • Release any unused resources or objects.
    • Use smaller images or lower quality settings if high resolution is not necessary.
  3. Check for Large Content: The specific slides that cause the error may contain large images or complex graphics. If possible, inspect those slides and consider simplifying them or breaking them into smaller parts.

  4. Use a Different Format: If TIFF is not a strict requirement, consider saving the slides in a different format that may consume less memory, such as PNG or JPEG.

  5. Test on Different Devices: Since you mentioned that the error occurs on a specific device, try running the same code on different devices or emulators to see if the issue persists.

Here is a sample code snippet that demonstrates how to convert a slide to an image with specified dimensions:

import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.TiffOptions;

public class ConvertSlideToImage {
    public static void main(String[] args) throws Exception {
        // Load the presentation
        Presentation presentation = new Presentation("path/to/presentation.pptx");

        // Set image options
        TiffOptions options = new TiffOptions();
        options.setImageSize(new Dimension(1536, 864)); // Adjust dimensions as needed

        // Save the slide as an image
        presentation.save("output_image.tiff", SaveFormat.Tiff, options);
        System.out.println("Slide converted to image successfully.");
    }
}

By following these suggestions, you should be able to mitigate the memory issues you are experiencing. If the problem persists, consider reaching out to Aspose support for further assistance.