本文介绍了的Andr​​oid内存不足错误加载从资源图像时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我在运行时使用一个线程加入ImageViews(约13)的布局。当我加载超过7或8个图像(970像素点¯x970像素,270KB)应用程序崩溃显示内存不足的错误。现在,当我加载相同的图像,而不是选择13种不同的图像则显示正常。而且当我降低了图像的大小(16像素点¯x16像素,770bytes)它显示的罚款。

因此​​,没有人知道允许什么最大内存加载图像时?什么是解决这个问题的最佳方式是什么?显然,人们所遇到的这个问题之前,解决其因装载图像画廊等。什么对这些图像加载到应用程序的最佳方法?任何帮助将大大AP preciated。

下面是如何我读从资源的形象。这是我从我通过阅读对象的列表服用drawableName循环中。这是所有内螺纹的onPostExecute(即新的AsyncTask())

 字符串DEFTYPE =绘;
串defPackage = this.getPackageName();
INT RESOURCEID = Activity.this.getResources()则getIdentifier(drawableName,DEFTYPE,defPackage)。
((ImageView的)view.findViewById(R.id.iv_image))setImageResource(RESOURCEID)。


解决方案

要解决问题的最快方法是从杰克沃顿使用毕加索库。它使code的一行来加载图像和管理缓存,转换,下载等。

下面是下载库链接: http://square.github.io/picasso/

您需要的所有code是:

  Picasso.with(上下文).load(http://i.imgur.com/DvpvklR.png).into(ImageView的);

它应该工作就像一个魅力:)

I have a layout which I'm adding ImageViews (about 13) to at run time using a single thread. When I load more than 7 or 8 images (970px x 970px, 270kb) the application crashes displaying an out of memory error. Now when I load the same image rather than choosing 13 different images it displays fine. And also when I reduce the size of the images (16px x 16px, 770bytes) it displays fine.

So does anyone know what the max memory allowed when loading images? What is the best way to get around this issue? Obviously people have come across this issue before and solved it due to loading image galleries etc.. what's the best way to load these images into the application? Any help would be greatly appreciated.

The following is how I'm reading the image from the resources. This is within a loop that I am taking the "drawableName" from a list of objects that I'm reading through. This is all within the onPostExecute of a thread (i.e. new AsyncTask())

String defType = "drawable";
String defPackage = this.getPackageName();
int resourceId = Activity.this.getResources().getIdentifier(drawableName, defType, defPackage);
((ImageView) view.findViewById(R.id.iv_image)).setImageResource(resourceId);

The fastest way to resolve your problem is to use the Picasso library from Jake Wharton. It enables in one line of code to load your image and manage caching, transformations, download etc..

Here is the link to download the library: http://square.github.io/picasso/

All the code you need is :

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

It should work like a charm :)

这篇关于的Andr​​oid内存不足错误加载从资源图像时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:33