[英]load image into imageview as a bitmap with Glide many time
I want to create an app like Instagram. 我想创建一个像Instagram这样的应用程序。 My users can select their own images from their storage, crop them and send them to server.
我的用户可以从存储中选择自己的图像,进行裁剪并将其发送到服务器。 I have a fragment with an
CropperImageView
in top and a RecyclerView
in the bottom. 我有一个片段,顶部是
CropperImageView
,底部是RecyclerView
。 I used this library for image cropping. 我使用该库进行图像裁剪。 When user clicks on an image in
RecyclerView
, I load that image as a bitmap and set that bitmap for CropperImageView
like the following 当用户单击
RecyclerView
的图像时,我将该图像作为位图加载并为CropperImageView
设置该位图,如下所示
Glide.with(getContext()).asBitmap().load(imageFilePath).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
cropperView.setImageBitmap(resource);
}
});
Because a user can click on Recyclerview image items many times. 因为用户可以多次单击Recyclerview图像项目。 I get
OutOfMemory
exception from Glide. 我从Glide获得
OutOfMemory
异常。 the following is Glide log in logcat. 以下是logcat中的Glide日志。
W/Glide: Load failed for /storage/4BC8-1B11/DCIM/Camera/20180520_210400.jpg with size [-2147483648x-2147483648]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 cause:
java.lang.OutOfMemoryError(Failed to allocate a 51121164 byte allocation with 16701336 free bytes and 15MB until OOM)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{FileInputStream->Bitmap->Bitmap}, LOCAL
There was 1 cause:
java.lang.OutOfMemoryError(Failed to allocate a 51121164 byte allocation with 16701336 free bytes and 15MB until OOM)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->Bitmap->Bitmap}
There was 1 cause:
java.lang.OutOfMemoryError(Failed to allocate a 51121164 byte allocation with 16701336 free bytes and 15MB until OOM)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class java.lang.OutOfMemoryError: Failed to allocate a 51121164 byte allocation with 16701336 free bytes and 15MB until OOM
Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{ParcelFileDescriptor->Bitmap->Bitmap}, LOCAL
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ParcelFileDescriptor->Bitmap->Bitmap}
Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{AssetFileDescriptor->Bitmap->Bitmap}, LOCAL
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{AssetFileDescriptor->Bitmap->Bitmap}
05-21 14:09:34.140 13837-13837/com.mediana.vip I/Glide: Root cause (1 of 1)
java.lang.OutOfMemoryError: Failed to allocate a 51121164 byte allocation with 16701336 free bytes and 15MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:977)
at android.graphics.Bitmap.createBitmap(Bitmap.java:948)
at android.graphics.Bitmap.createBitmap(Bitmap.java:915)
at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.createBitmap(LruBitmapPool.java:149)
at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.get(LruBitmapPool.java:131)
at com.bumptech.glide.load.resource.bitmap.TransformationUtils.rotateImageExif(TransformationUtils.java:329)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decodeFromWrappedStreams(Downsampler.java:314)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decode(Downsampler.java:207)
at com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder.decode(StreamBitmapDecoder.java:62)
at com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder.decode(StreamBitmapDecoder.java:18)
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.SourceGenerator.onDataReady(SourceGenerator.java:112)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onDataReady(MultiModelLoader.java:133)
at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:52)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:97)
at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:299)
at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:269)
at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446)
How can I solve this problem? 我怎么解决这个问题?
I solved my problem with setting width and height for SimpleTarge
. 我通过为
SimpleTarge
设置宽度和高度解决了我的问题。 Because I didn't set width and height for SimpleTarget
, It considered the original size of image. 因为我没有为
SimpleTarget
设置width和height,所以它考虑了图像的原始大小。 When I changed the code as following, I didn't get OutOfMemory exception from Glide. 当我如下更改代码时,我没有从Glide获得OutOfMemory异常。
Glide.with(getContext()).asBitmap().load(imageFilePath).into(new SimpleTarget<Bitmap>(cropperView.getWidth(),cropperView.getHeight()) {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
cropperView.setImageBitmap(resource);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.