gpt4 book ai didi

java - 从 Glide 下载大图像时出现内存不足错误

转载 作者:行者123 更新时间:2023-12-01 16:46:20 24 4
gpt4 key购买 nike

这是我尝试下载大图像的代码..

RequestOptions requestOptions = new RequestOptions();
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
requestOptions.skipMemoryCache(true);
Glide.with(this)
.load(rogImage)
.apply(requestOptions)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
ProgressBarHandler.unloadProgressScreen(getActivity());
return false;
}
})
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
tempImageView.setImageDrawable(resource)
}

});

我大部分时间都在 onLoad 失败的方法中结束并出现 OutOfMemoryError。

当我检查设备文件资源管理器时,图像正在下载,但第二次没有加载到 ImageView 中,第一次它非常适合大图像。

我在第二次调用时重复相同的代码。

下面是android studio中给出的实际错误消息。

Glide: Root cause (1 of 13)
java.lang.OutOfMemoryError: Failed to allocate a 210639956 byte allocation with 16777216 free bytes and 197MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:975)
at android.graphics.Bitmap.createBitmap(Bitmap.java:946)
at android.graphics.Bitmap.createBitmap(Bitmap.java:913)
at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.createBitmap(LruBitmapPool.java:149)
at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.getDirty(LruBitmapPool.java:142)
at com.bumptech.glide.load.resource.bitmap.Downsampler.setInBitmap(Downsampler.java:688)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decodeFromWrappedStreams(Downsampler.java:297)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decode(Downsampler.java:207)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decode(Downsampler.java:160)
at com.bumptech.glide.load.resource.bitmap.ByteBufferBitmapDecoder.decode(ByteBufferBitmapDecoder.java:33)
at com.bumptech.glide.load.resource.bitmap.ByteBufferBitmapDecoder.decode(ByteBufferBitmapDecoder.java:16)
at com.bumptech.glide.load.engine.DecodePath.decodeResourceWithList(DecodePath.java:72)
at com.bumptech.glide.load.engine.DecodePath.decodeResource(DecodePath.java:55)
at com.bumptech.glide.load.engine.DecodePath.decode(DecodePath.java:45)
at com.bumptech.glide.load.engine.LoadPath.loadWithExceptionList(LoadPath.java:58)
at com.bumptech.glide.load.engine.LoadPath.load(LoadPath.java:43)
at com.bumptech.glide.load.engine.DecodeJob.runLoadPath(DecodeJob.java:498)
at com.bumptech.glide.load.engine.DecodeJob.decodeFromFetcher(DecodeJob.java:469)
at com.bumptech.glide.load.engine.DecodeJob.decodeFromData(DecodeJob.java:455)
at com.bumptech.glide.load.engine.DecodeJob.decodeFromRetrievedData(DecodeJob.java:407)
at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherReady(DecodeJob.java:376)
at com.bumptech.glide.load.engine.DataCacheGenerator.onDataReady(DataCacheGenerator.java:95)
at com.bumptech.glide.load.model.ByteBufferFileLoader$ByteBufferFetcher.loadData(ByteBufferFileLoader.java:74)
at com.bumptech.glide.load.engine.DataCacheGenerator.startNext(DataCacheGenerator.java:75)
at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:299)
at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:266)
at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446)

您可以查看屏幕截图,其中显示图像已下载并存储在设备中。

enter image description here

最佳答案

将其添加到 list 中

  android:largeHeap="true"

另外添加这是app.gradle

 dexOptions {// Prevent OutOfMemory with MultiDex during the build phase
javaMaxHeapSize "4g"
}

关于java - 从 Glide 下载大图像时出现内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49894955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com