【问题标题】:load image into imageview as a bitmap with Glide many time多次使用 Glide 将图像作为位图加载到 imageview 中
【发布时间】:2018-05-21 10:23:24
【问题描述】:

我想创建一个类似 Instagram 的应用。我的用户可以从他们的存储中选择他们自己的图像,裁剪它们并将它们发送到服务器。我有一个片段,顶部有一个CropperImageView,底部有一个RecyclerView。我使用this library 进行图像裁剪。当用户单击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);
            }
        });

因为用户可以多次单击 Recyclerview 图像项。我从 Glide 得到 OutOfMemory 异常。以下是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)

我该如何解决这个问题?

【问题讨论】:

    标签: android bitmap out-of-memory android-glide


    【解决方案1】:

    我解决了为SimpleTarge 设置宽度和高度的问题。因为我没有为SimpleTarget设置宽度和高度,它考虑了图像的原始大小。当我更改如下代码时,我没有从 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);
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2015-10-30
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 2019-09-08
      • 1970-01-01
      • 2017-07-05
      相关资源
      最近更新 更多