i have a listview that shows diferent data from my database. this works perfeclty. however i can only have 3 items in my list becouse i run out of memory since im loading the full images. this is the code i have now that is working, but gives out of memory when more then three list items are in the list.
String[] from = new String[] { "NAME", "DATE", "WEIGHT", "IMAGE_PATH"};
int[] to = new int[] { R.id.name, R.id.date, R.id.weight, R.id.list_image};
contactAdapter = new SimpleCursorAdapter(
Mylistview.this, R.layout.list_row, null, from, to);
setListAdapter(contactAdapter);
what i want to do is shrink the images to 72×72 before loading them in the list. Below is a code that is not working the way i want, but it gives you an idea of what i want 🙂
list_image = (ImageView) findViewById(R.id.list_image);
String[] from = new String[] { "NAME", "DATE", "WEIGHT", "IMAGE_PATH"};
int[] to = new int[] { R.id.name, R.id.date, R.id.weight};
contactAdapter = new SimpleCursorAdapter(
Mylistview.this, R.layout.list_row, null, from, to);
setListAdapter(contactAdapter);
String icon = "IMAGE_PATH"
Bitmap bmp = BitmapFactory.decodeStream(icon);
Bitmap bm = Bitmap.createScaledBitmap(bmp, 72, 72, false);
list_image.setImageBitmap(bm);
i want the String icon to get the image path from “IMAGE_PATH” that is loaded from the database in the string array.
is there a way to do this, or do i need to find other ways to populate my listview with both images and small icons? perhaps when i take a picture i should save the image in two places. one real image and one icon and put the icon path to the database?
thank you
This is the code and the error code after doing what Vishva Patel helped with.
BitmapScaler scaler = null;
try {
scaler = new BitmapScaler(new File("IMAGE_PATH"), 72);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list_image.setImageBitmap(scaler.getScaled());
08-08 22:23:15.973: E/AndroidRuntime(2947): FATAL EXCEPTION: main
08-08 22:23:15.973: E/AndroidRuntime(2947): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.xflash.myapp/se.xflash.myapp.myapp}: java.lang.RuntimeException: Unable to start activity ComponentInfo{se.xflash.myapp/se.xflash.myapp.MyNames}: java.lang.NullPointerException
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.os.Looper.loop(Looper.java:137)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.ActivityThread.main(ActivityThread.java:4507)
08-08 22:23:15.973: E/AndroidRuntime(2947): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 22:23:15.973: E/AndroidRuntime(2947): at java.lang.reflect.Method.invoke(Method.java:511)
08-08 22:23:15.973: E/AndroidRuntime(2947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-08 22:23:15.973: E/AndroidRuntime(2947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-08 22:23:15.973: E/AndroidRuntime(2947): at dalvik.system.NativeStart.main(Native Method)
08-08 22:23:15.973: E/AndroidRuntime(2947): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{se.xflash.myapp/se.xflash.myapp.MyNames}: java.lang.NullPointerException
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1809)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.widget.TabHost.setCurrentTab(TabHost.java:346)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.widget.TabHost.addTab(TabHost.java:236)
08-08 22:23:15.973: E/AndroidRuntime(2947): at se.xflash.myapp.myapp.onCreate(myapp.java:45)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.Activity.performCreate(Activity.java:4465)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
08-08 22:23:15.973: E/AndroidRuntime(2947): ... 11 more
08-08 22:23:15.973: E/AndroidRuntime(2947): Caused by: java.lang.NullPointerException
08-08 22:23:15.973: E/AndroidRuntime(2947): at se.xflash.myapp.BitmapScaler.<init>(BitmapScaler.java:42)
08-08 22:23:15.973: E/AndroidRuntime(2947): at se.xflash.myapp.MyNames.onCreate(MyNames.java:56)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.Activity.performCreate(Activity.java:4465)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-08 22:23:15.973: E/AndroidRuntime(2947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
08-08 22:23:15.973: E/AndroidRuntime(2947): ... 21 more
1.) The easiest solution would be to use the class mentioned in this blogpost BitmapScaler.java : http://zerocredibility.wordpress.com/2011/01/27/android-bitmap-scaling/ , it works excellently and solves the out of memory exception issue. All you have to do is provide the file where your image is saved as a parameter to the constructor along with the scaled width that you want (72 in your case). Here’s the code you’d have to use in your specific case:
2.) Yes, one of the solutions would be to scale the bitmap when you take the picture and save it as icon-yourimagename.jpg along with your original image (if you need the original size image that you took)
3.) Use bm.recycle() once your have assigned bm to your list_image to ensure that the bitmaps are garbage collected and that some memory is freed. This MIGHT fix the memory error.