编辑:
>在我的应用程序中,我在主页上加载了300多张图像.我用滑动来加载图像.我出现了内存错误.
我在清单中使用了大堆true:
androID:largeHeap="true"
滑翔版:
compile 'com.github.bumptech.glIDe:glIDe:3.7.0'
设备/ AndroID版本:
Nexus Device 6.0版本
Every images I’m getting from Json would be 800kb to 1mb.
activity_layout:
<relativeLayout androID:ID="@+ID/home_layout_bottom" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_below="@+ID/home_layout_top_recycler" androID:layout_margin="5dp"> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/rv_List_tab_home_recycler" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:clipTopadding="false" androID:scrollbars="vertical" androID:visibility="visible" /> <TextVIEw androID:ID="@+ID/no_user_posts_item_tv_recycler" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/rv_List_tab_home_recycler" androID:layout_margintop="80dp" androID:layout_centerHorizontal="true" androID:text="@string/txt_no_posts_available" androID:textcolor="@color/txt_common_black" androID:textSize="@dimen/txt_size" /></relativeLayout>
适配器代码:
@OverrIDepublic VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEwHolder holder = null; final HomePostItems rowItem = getItem(position); LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if (convertVIEw == null) { convertVIEw = mInflater.inflate(R.layout.lv_adapter_post_items_layout, null); holder = new VIEwHolder(); holder.ivPostedImage = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.iv_posted_img); convertVIEw.setTag(holder); } else { holder = (VIEwHolder) convertVIEw.getTag(); } .................. GlIDe.with(context).load(rowItem.getPosteduserpostimage()) .placeholder(R.drawable.golive_load_image).error(R.drawable.golive_cancel_image) .overrIDe(600, 200) .into(holder.ivPostedImage);
adapter_layout.xml:
<relativeLayout androID:ID="@+ID/rl_lv_user_post_adapter_img_holder_home" androID:layout_wIDth="match_parent" androID:layout_height="300dp" androID:layout_marginleft="1dp" androID:layout_marginRight="1dp" androID:layout_below="@+ID/tv_user_posted_msg_post_items_home" > <ImageVIEw androID:ID="@+ID/iv_posted_img_home" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_centerInParent="true" androID:scaleType="fitXY" androID:background="#ffffff" androID:contentDescription="@string/cont_desc"/></relativeLayout>
logcat的:
Request threw uncaught throwablejava.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: Failed to allocate a 6365196 byte allocation with 865912 free bytes and 845KB until OOMat java.util.concurrent.FutureTask.report(FutureTask.java:94)at java.util.concurrent.FutureTask.get(FutureTask.java:164)at com.bumptech.glIDe.load.engine.executor.FifoPriorityThreadPoolExecutor.afterExecute(FifoPriorityThreadPoolExecutor.java:96)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1121)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.FifoPriorityThreadPoolExecutor$DefaultThreadFactory.run(FifoPriorityThreadPoolExecutor.java:118)Caused by: java.lang.OutOfMemoryError: Failed to allocate a 6365196 byte allocation with 865912 free bytes and 845KB until OOMat dalvik.system.vmruntime.newNonMovableArray(Native Method)at androID.graphics.BitmapFactory.nativeDecodeStream(Native Method)at androID.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)at androID.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)at com.bumptech.glIDe.load.resource.bitmap.Downsampler.decodeStream(Downsampler.java:329)at com.bumptech.glIDe.load.resource.bitmap.Downsampler.downsampleWithSize(Downsampler.java:220)at com.bumptech.glIDe.load.resource.bitmap.Downsampler.decode(Downsampler.java:153)at com.bumptech.glIDe.load.resource.bitmap.StreamBitmapDecoder.decode(StreamBitmapDecoder.java:50)at com.bumptech.glIDe.load.resource.bitmap.StreamBitmapDecoder.decode(StreamBitmapDecoder.java:19)at com.bumptech.glIDe.load.resource.bitmap.ImageVIDeoBitmapDecoder.decode(ImageVIDeoBitmapDecoder.java:39)at com.bumptech.glIDe.load.resource.bitmap.ImageVIDeoBitmapDecoder.decode(ImageVIDeoBitmapDecoder.java:20)at com.bumptech.glIDe.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decodeBitmapWrapper(GifBitmapWrapperResourceDecoder.java:121)at com.bumptech.glIDe.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decodeStream(GifBitmapWrapperResourceDecoder.java:94)at com.bumptech.glIDe.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decode(GifBitmapWrapperResourceDecoder.java:71)at com.bumptech.glIDe.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decode(GifBitmapWrapperResourceDecoder.java:61)at com.bumptech.glIDe.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decode(GifBitmapWrapperResourceDecoder.java:22)at com.bumptech.glIDe.load.engine.DecodeJob.decodeFromSourceData(DecodeJob.java:190)at com.bumptech.glIDe.load.engine.DecodeJob.decodeSource(DecodeJob.java:177)at com.bumptech.glIDe.load.engine.DecodeJob.decodeFromSource(DecodeJob.java:128)at com.bumptech.glIDe.load.engine.EngineRunnable.decodeFromSource(EngineRunnable.java:122)at com.bumptech.glIDe.load.engine.EngineRunnable.decode(EngineRunnable.java:101)at com.bumptech.glIDe.load.engine.EngineRunnable.run(EngineRunnable.java:58)at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)at java.util.concurrent.FutureTask.run(FutureTask.java:237)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.FifoPriorityThreadPoolExecutor$DefaultThreadFactory.run(FifoPriorityThreadPoolExecutor.java:118)
我不知道如何解决这个OOM问题.如果您已经熟悉此问题,请分享您的建议.
解决方法:
这不是您问题的精确解决方案,但是您需要在使用GlIDe在列表中加载图像时记住这些事项.
您问题的主要威胁部分是图像大小.你得到的图像几乎是1mb!实际上,这对于将它们显示在具有300个项目的列表中来说太大了.因此,如果您也在服务器端,它总是建议使用几种不同大小的图像.
例如,如果显示朋友列表及其个人资料图片,我建议您先从服务器获取整个列表.然后获取所有配置文件图像并将其存储在本地.然后填充ListVIEw.并且最重要的部分是在将用户的个人资料图片上传到服务器时,在上传之后,服务器需要保持其几个大小,例如,低,中,高分辨率版本.因此,在为ListVIEw提供配置文件图片网址时,服务器可能会提供低分辨率的图像,因为它们最有可能用于缩略图.
使用RecyclerVIEw而不是ListVIEw也是一个很好的调用.但是当你处于低端设备时,它无法解决你在这里遇到的问题.
OMM与您无关可以以编程方式解决.您需要将图像调整为较低版本.
您也可以查看GlIDe的caching mechanism.我建议你使用缓存策略,这样每次你不必从服务器加载图像.
祝好运.
总结以上是内存溢出为你收集整理的java – 内存不足错误在Glide中加载更多图像时全部内容,希望文章能够帮你解决java – 内存不足错误在Glide中加载更多图像时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)