问题
I have an Activity with a grid layout where display images from SD folders. When I select a folder, all images are added to a String list (clean when folder is changed), and images construction is in getView() method, using these strings from the list.
It works properly for 5 or 6 times (images from folder are displayed), but then I change the folder and I get an 'outofmemoryerror' in the getView() from the custom BaseAdapter (see code line), but I don't understand my leak problem...
Images are 30-100 kB each one.
This is the code:
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;
}
Trace Log:
04-21 10:10:50.697: E/art(1665): Throwing OutOfMemoryError "Failed to allocate a 1000012 byte allocation with 732188 free bytes and 715KB until OOM"
04-21 10:11:31.208: E/AndroidRuntime(1665): FATAL EXCEPTION: main 04-21 10:11:31.208: E/AndroidRuntime(1665): Process: com.apps.apppictos, PID: 1665 04-21 10:11:31.208: E/AndroidRuntime(1665): java.lang.OutOfMemoryError: Failed to allocate a 1000012 byte allocation with 732188 free bytes and 715KB until 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): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.graphics.drawable.Drawable.createFromPath(Drawable.java:1143) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.ImageView.resolveUri(ImageView.java:782) 04-21 10:11:31.208: E/AndroidRuntime(1665): at 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): at android.widget.AbsListView.obtainView(AbsListView.java:2347) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.GridView.makeAndAddView(GridView.java:1433) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.GridView.makeRow(GridView.java:361) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.GridView.fillDown(GridView.java:302) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.GridView.fillSpecific(GridView.java:582) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.GridView.layoutChildren(GridView.java:1287) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.AbsListView.onLayout(AbsListView.java:2151) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.View.layout(View.java:15671) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.View.layout(View.java:15671) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.View.layout(View.java:15671) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.View.layout(View.java:15671) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.View.layout(View.java:15671) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.Choreographer.doCallbacks(Choreographer.java:580) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.Choreographer.doFrame(Choreographer.java:550) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.os.Handler.handleCallback(Handler.java:739) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.os.Handler.dispatchMessage(Handler.java:95) 04-21 10:11:31.208: E/AndroidRuntime(1665): at android.os.Looper.loop(Looper.java:135) 04-21 10:11:31.208: E/AndroidRuntime(1665): at 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): Clamp target GC heap from 49MB to 48MB
回答1:
Yes, using images may cause outOfMemory on your device. The allocated memory differs from device to device.
Two approaches:
Use LruCache class to optimize memory usage and caching
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Use Picasso a powerful image downloading and caching library for Android
http://square.github.io/picasso/
回答2:
This also happens when someone inflate a CustomView like:
inflate(getContext, R.layout.my_custom_view_layout, this)
instead of:
inflate(getContext, R.layout.my_custom_view_layout, null)
来源:https://stackoverflow.com/questions/29768727/outofmemoryerror-in-the-getview-from-custom-baseadapter