将图像作为位图加载到imageview中,使用Glide很多次

时间:2018-05-21 10:23:24

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

我想创建一个像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例外。以下是Glide登录logcat。

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)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我通过设置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);
        }
    });
相关问题