簡體   English   中英

多次使用Glide將圖像作為位圖加載到imageview中

[英]load image into imageview as a bitmap with Glide many time

我想創建一個像Instagram這樣的應用程序。 我的用戶可以從存儲中選擇自己的圖像,進行裁剪並將其發送到服務器。 我有一個片段,頂部是CropperImageView ,底部是RecyclerView 我使用該庫進行圖像裁剪。 當用戶單擊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)

我怎么解決這個問題?

我通過為SimpleTarge設置寬度和高度解決了我的問題。 因為我沒有為SimpleTarget設置width和height,所以它考慮了圖像的原始大小。 當我如下更改代碼時,我沒有從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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM