我有一个带有网格布局的活动,其中显示来自SD文件夹的图像。当我选择一个文件夹时,所有图像都被添加到一个字符串列表中(当文件夹被更改时清理),图像构造在getView()方法中,使用列表中的这些字符串。
它可正常工作5或6次(显示文件夹中的图像),但随后我更改了文件夹,我得到了一个“错误的错误”。在自定义BaseAdapter的getView()中(参见代码行),但我不了解我的泄漏问题...
图像各为30-100 kB。
这是代码:
private ArrayList<String> itemList = new ArrayList<String>();
...
public void add(String path) {
itemList.add(path);
}
public void clean() {
itemList.clear();
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{
int iImageSize = mContext.getResources().getInteger(R.integer.grid_image_size);
int iImagePadding = mContext.getResources().getInteger(R.integer.grid_image_padding);
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(iImageSize, iImageSize));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(iImagePadding,iImagePadding,iImagePadding,iImagePadding);
}
else
imageView = (ImageView) convertView;
String sImagePath = itemList.get(position);
imageView.setTag(sImagePath);
imageView.setImageURI(Uri.parse(sImagePath)); // Crash HERE
return imageView;
}
跟踪日志:
04-21 10:10:50.697:E / art(1665):抛出OutMforyError&#34;无法分配带有732188个空闲字节和715KB的1000012字节分配,直到OOM&#34;
04-21 10:11:31.208:E / AndroidRuntime(1665):致命异常:主要 04-21 10:11:31.208:E / AndroidRuntime(1665):进程:com.apps.apppictos,PID:1665 04-21 10:11:31.208:E / AndroidRuntime(1665):java.lang.OutOfMemoryError:无法分配带有732188个空闲字节的1000012字节分配和715KB直到OOM 04-21 10:11:31.208:E / AndroidRuntime(1665):at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.graphics.BitmapFactory.nativeDecodeStream(Native方法) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.graphics.drawable.Drawable.createFromPath(Drawable.java:1143) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.ImageView.resolveUri(ImageView.java:782) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.ImageView.setImageURI(ImageView.java:429) 04-21 10:11:31.208:E / AndroidRuntime(1665):at com.apps.apppictos.ImageAdapter.getView(ImageAdapter.java:93) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.AbsListView.obtainView(AbsListView.java:2347) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.GridView.makeAndAddView(GridView.java:1433) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.GridView.makeRow(GridView.java:361) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.GridView.fillDown(GridView.java:302) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.GridView.fillSpecific(GridView.java:582) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.GridView.layoutChildren(GridView.java:1287) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.AbsListView.onLayout(AbsListView.java:2151) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.View.layout(View.java:15671) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.ViewGroup.layout(ViewGroup.java:5038) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.View.layout(View.java:15671) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.ViewGroup.layout(ViewGroup.java:5038) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.FrameLayout.onLayout(FrameLayout.java:514) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.View.layout(View.java:15671) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.ViewGroup.layout(ViewGroup.java:5038) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.View.layout(View.java:15671) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.ViewGroup.layout(ViewGroup.java:5038) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.widget.FrameLayout.onLayout(FrameLayout.java:514) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.View.layout(View.java:15671) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.ViewGroup.layout(ViewGroup.java:5038) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5885) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.Choreographer.doCallbacks(Choreographer.java:580) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.Choreographer.doFrame(Choreographer.java:550) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.os.Handler.handleCallback(Handler.java:739) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.os.Handler.dispatchMessage(Handler.java:95) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.os.Looper.loop(Looper.java:135) 04-21 10:11:31.208:E / AndroidRuntime(1665):在android.app.ActivityThread.main(ActivityThread.java:5257) 04-21 10:11:31.208:E / AndroidRuntime(1665):at java.lang.reflect.Method.invoke(Native Method) 04-21 10:11:31.208:E / AndroidRuntime(1665):at java.lang.reflect.Method.invoke(Method.java:372) 04-21 10:11:31.208:E / AndroidRuntime(1665):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 04-21 10:11:31.208:E / AndroidRuntime(1665):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 04-21 10:11:39.427:I / art(1665):将目标GC堆从49MB夹到48MB
答案 0 :(得分:0)
是的,使用图片可能会导致设备上出现outOfMemory。分配的内存因设备而异。
两种方法:
使用LruCache类优化内存使用和缓存
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
使用Picasso强大的Android图像下载和缓存库
答案 1 :(得分:-1)
当有人给CustomView充气时会发生这种情况:
inflate(getContext, R.layout.my_custom_view_layout, this)
而不是:
inflate(getContext, R.layout.my_custom_view_layout, null)