在数据库列表视图中显示图标

时间:2012-08-08 18:11:14

标签: android database image listview

我有一个列表视图,显示我的数据库中的不同数据。这是完美的。但是我的列表中只能有3个项目,因为我加载完整的图像后我的内存耗尽。这是我现在正在使用的代码,但是当列表中有三个以上的列表项时会给出内存不足。

  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);

我想要做的是将图像缩小到72x72,然后再将它们加载到列表中。下面的代码没有按我想要的方式工作,但是它让你知道我想要的东西:)

  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);

我希望String图标从字符串数组中的数据库加载的“IMAGE_PATH”中获取图像路径。

有没有办法做到这一点,还是我需要找到其他方法来填充我的listview与图像和小图标?也许当我拍照时,我应该将图像保存在两个地方。一个真实的图像和一个图标,并将图标路径放到数据库?

谢谢

这是Vishva Patel帮助后做的代码和错误代码。

       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 个答案:

答案 0 :(得分:1)

1。)最简单的解决方案是使用本博客文章中提到的类BitmapScaler.java:http://zerocredibility.wordpress.com/2011/01/27/android-bitmap-scaling/,它可以很好地解决内存异常问题。您所要做的就是将保存图像的文件作为参数提供给构造函数以及您想要的缩放宽度(在您的情况下为72)。以下是您在特定情况下必须使用的代码:

BitmapScaler scaler = new BitmapScaler(new File("IMAGE_PATH"), 72);
list_image.setImageBitmap(scaler.getScaled());

2。)是的,其中一个解决方案是在拍摄照片时缩放位图,并将其与原始图像一起保存为icon-yourimagename.jpg(如果您需要拍摄的原始尺寸图像)< / p>

3.。)一旦将bm分配给list_image,请使用bm.recycle()以确保位图被垃圾收集并释放一些内存。这可能会修复内存错误。