Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException: Attempt to invoke virtual method 'java.nio.Buffer java.nio.ByteBuffer.rewind()' on a null object reference #228

Open
panpf opened this issue Nov 12, 2024 · 0 comments

Comments

@panpf
Copy link

panpf commented Nov 12, 2024

Issue Info

Info Value
Device Info arm64 模拟器
System Version api 21
APNG4Android Library Version 3.0.1

Issue Description and Steps

我尝试将 awebp 模块集成到我的项目中,用于在 api 28 以下解码 webp 的第一帧,代码如下:

override fun decode(): DecodeResult {
    val streamLoader = dataSource.toStreamLoader(requestContext.request.context)
    val webpDecoder = WebPDecoder(streamLoader, null)
    val bounds = webpDecoder.bounds
    val imageInfo = ImageInfo(
        width = bounds.width(),
        height = bounds.height(),
        mimeType = "image/webp"
    )
    val resize = requestContext.computeResize(imageInfo.size)
    val inSampleSize = calculateSampleSize(
        imageSize = imageInfo.size,
        targetSize = resize.size,
        smallerSizeMode = resize.precision.isSmallerSizeMode()
    )
    val dstSize = Size(
        width = ceil(imageInfo.width / inSampleSize.toFloat()).toInt(),
        height = ceil(imageInfo.height / inSampleSize.toFloat()).toInt()
    )
    webpDecoder.setDesiredSize(dstSize.width, dstSize.height)
    val bitmap = webpDecoder.getFrameBitmap(0)
    val decodeResult = DecodeResult(
        image = bitmap.asImage(),
        imageInfo = imageInfo,
        dataFrom = dataSource.dataFrom,
        resize = resize,
        transformeds = null,
        extras = null,
    )
    val resizeResult = decodeResult.resize(resize)
    return resizeResult
}

fun DataSource.toStreamLoader(context: Context): Loader {
    return when (this) {
        is FileDataSource -> {
            FileLoader(path.toString())
        }

        is ByteArrayDataSource -> {
            object : ByteBufferLoader() {
                override fun getByteBuffer(): ByteBuffer {
                    return ByteBuffer.wrap(data)
                }
            }
        }

        is ContentDataSource -> {
            object : StreamLoader() {
                override fun getInputStream(): InputStream {
                    return context.contentResolver.openInputStream(contentUri)
                        ?: throw IOException("Invalid content uri: $contentUri")
                }
            }
        }

        is AssetDataSource -> {
            AssetStreamLoader(context, fileName)
        }

        is ResourceDataSource -> {
            ResourceStreamLoader(context, resId)
        }

        else -> {
            throw IllegalArgumentException("Unsupported DataSource: ${this::class}")
        }
    }
}

上述代码抛出以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.nio.Buffer java.nio.ByteBuffer.rewind()' on a null object reference
	at com.github.penfeizhou.animation.webp.decode.WebPDecoder.renderFrame(WebPDecoder.java:131)
	at com.github.penfeizhou.animation.decode.FrameSeqDecoder.step(FrameSeqDecoder.java:510)
	at com.github.penfeizhou.animation.decode.FrameSeqDecoder.getFrameBitmap(FrameSeqDecoder.java:552)
	at com.github.panpf.sketch.sample.util.PenfeiZhouAnimatedWebpFirstFrameDecoder.decode(PenfeiZhouAnimatedWebpFirstFrameDecoder.kt:73)
	at com.github.panpf.sketch.decode.internal.EngineDecodeInterceptor.intercept-gIAlu-s(EngineDecodeInterceptor.kt:45)
	at com.github.panpf.sketch.decode.internal.DecodeInterceptorChain.proceed-IoAF18A(DecodeInterceptorChain.kt:48)
	at com.github.panpf.sketch.sample.image.PaletteDecodeInterceptor.intercept-gIAlu-s(PaletteDecodeInterceptor.kt:17)
	at com.github.panpf.sketch.decode.internal.DecodeInterceptorChain.proceed-IoAF18A(DecodeInterceptorChain.kt:48)
	at com.github.panpf.sketch.transform.internal.TransformationDecodeInterceptor.intercept-gIAlu-s(TransformationDecodeInterceptor.kt:38)
	at com.github.panpf.sketch.decode.internal.DecodeInterceptorChain.proceed-IoAF18A(DecodeInterceptorChain.kt:48)
	at com.github.panpf.sketch.cache.internal.ResultCacheDecodeInterceptor.intercept-gIAlu-s(ResultCacheDecodeInterceptor.kt:72)
	at com.github.panpf.sketch.decode.internal.DecodeInterceptorChain.proceed-IoAF18A(DecodeInterceptorChain.kt:48)
	at com.github.panpf.sketch.request.PauseLoadWhenScrollingDecodeInterceptor.intercept-gIAlu-s(PauseLoadWhenScrollingDecodeInterceptor.kt:68)
	at com.github.panpf.sketch.decode.internal.DecodeInterceptorChain.proceed-IoAF18A(DecodeInterceptorChain.kt:48)
	at com.github.panpf.sketch.request.internal.EngineRequestInterceptor$intercept$decodeResult$1.invokeSuspend(EngineRequestInterceptor.kt:47)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:101)
	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:113)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:589)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:823)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:720)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:707)

测试发现注释掉 webpDecoder.setDesiredSize(dstSize.width, dstSize.height) 这行就不会抛异常

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant