cristian franco cristian franco - 1 month ago 9
Android Question

java.lang.OutOfMemoryError In ListView

i have a problem with listView and images, when set images in a moment make me a java.lang.OutOfMemoryError, this is my implementation of the ArrayAdapter in getview

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder; // view lookup cache stored in tag
if(convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category_list_item, parent, false);
viewHolder.image = (ImageView) convertView.findViewById(R.id.Icon);
viewHolder.name = (TextView) convertView.findViewById(R.id.nombreCategoriaTextView);
viewHolder.name.setText(categories.getCategories(position).getName());
try {
DataBaseImageManager manager = new DataBaseImageManager(context);
Bitmap mIcon11 = manager.getListByName(categories.getCategories(position).getURLImage());
if (mIcon11 == null)
new DownloadImageTask(viewHolder.image, context)
.execute(categories.getCategories(position).getURLImage());
else {
(viewHolder.image).setImageBitmap(mIcon11);
}
}catch (Exception e){}
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
return convertView;
}


i am putting my images in sqlIte and i decodified the bitmap from ByteArray the implementation is the next

public Bitmap getListByName(String name) {
Bitmap bmp = null;
try {
DataSource.open();
List<DatosImage> datos = DataSource.obtenerRegistros(name);
for (DatosImage dato : datos) {
bmp = BitmapFactory.decodeByteArray(dato.getValue(), 0, dato.getValue().length);
}
DataSource.close();
} catch (Exception e) {
e.printStackTrace();
}
return bmp;
}


which can be the soulution from my problem or whats is worng with my code?

the error is the nextFATAL EXCEPTION: main
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: Process: mx.com.sithec.cic, PID: 19660
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: java.lang.OutOfMemoryError
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:500)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:523)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at mx.com.sithec.cic.ImageHelper.DataBaseImageManager.getListByName(DataBaseImageManager.java:31)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at mx.com.sithec.cic.Adapters.CompanyAdapter.getView(CompanyAdapter.java:102)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2283)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.widget.ListView.makeAndAddView(ListView.java:1790)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.widget.ListView.fillUp(ListView.java:725)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.widget.ListView.fillGap(ListView.java:664)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5423)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4534)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:574)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:543)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5070)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
11-07 12:31:24.490 19660-19660/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)

Answer

The for loop in getListByName() seems odd. You might be creating multiple bitmaps (that alone could cause the OutOfMemoryError exception if you have many bitmaps in a single run) but are only returning one.

What you should probably do is something like:

public Bitmap getListByName(String name) {
    try {
        DataSource.open();
        List<DatosImage> datos = DataSource.obtenerRegistros(name);
        for (DatosImage dato : datos) {
             Bitmap bmp = BitmapFactory.decodeByteArray(dato.getValue(), 0, dato.getValue().length);
             if (bmp != null) {
                 DataSource.close();
                 return bmp;
             }
        }
        DataSource.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}