我有以下代码:
list_home = (ListView) findViewById (R.id.list_home);
cursor2 = baza5.query("igralci", new String[] {"slika","ime", "priimek", "pozicija","visina","_id"},
"id_ekipe like " + id_izbrane_ekipe_domaci+" AND " +
"je_izbran like " + pomozni,
null, null, null, null);
adapter2 = new SimpleCursorAdapter(
this,
R.layout.igralci_ekipe,
cursor2,
new String[] {"slika","ime","priimek","pozicija","visina","_id"},
new int[] {R.id.slika_igralca,R.id.ime_igralca,R.id.priimek_igralca,R.id.pozicija_igralca,
R.id.visina_igralca,R.id.id});
list_home.setAdapter(adapter2);
还有我的 igralci_ekipe.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8px">
<ImageView
android:id="@+id/slika_igralca"
android:layout_gravity="left"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/ime_igralca"
android:layout_width="wrap_content"
android:textColor="#0000ff"
android:textSize="15dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/slika_igralca" />
<TextView
android:id="@+id/priimek_igralca"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ff"
android:textSize="15dp"
android:layout_marginLeft="16px"
android:layout_toRightOf="@id/ime_igralca" />
<TextView
android:id="@+id/pozicija_igralca"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ff"
android:textSize="15dp"
android:layout_marginLeft="16px"
android:layout_toRightOf="@id/priimek_igralca"/>
<TextView
android:id="@+id/visina_igralca"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ff"
android:textSize="15dp"
android:layout_marginLeft="16px"
android:layout_toRightOf="@id/pozicija_igralca"/>
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:textColor="#0000ff"
android:textSize="1dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16px"
android:layout_toRightOf="@id/visina_igralca"/>
</RelativeLayout>
数据库字段 slika(picture) 是我的图片在 sdcard 上的字符串路径。
这可以正常工作并且可以正确显示图片,但是当要加载 3 张或更多图片时,出现内存错误。
Is there a way I could use picture bitmap like this:
int targetW = slika_igralca.getWidth();
int targetH = slika_igralca.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath, bmOptions);
slika_igralca.setImageBitmap(bitmap);
然后作为字符串使用类似这一行的内容:slika_igralca.setImageBitmap(bitmap);