我從數據庫中獲取圖像以便將自定義的ListView放入圖像。Android獲取Blob自定義ListView
我的應用程序不崩潰,但圖像不衝擊片雷管。我從日誌中得到了這個錯誤:
E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /[[email protected]: open failed: ENOENT (No such file or directory)
對不起,我會嘗試清楚我的問題。
哪裏是我的代碼:
DBhelper.java
該函數返回我MatrixCursor的所有數據。
public MatrixCursor retriveCntDetails() throws SQLException {
MatrixCursor mMatrixCursor = new MatrixCursor(new String[]{"_id", "name", "number", "photo"});
Cursor cur = mDb.rawQuery("select * from " + CONTACT_TABLE + "", null);
if (cur.moveToFirst()) {
do {
mMatrixCursor.addRow(new Object[]{Long.toString(cur.getLong(cur.getColumnIndex(CNT_ID))),
cur.getString(cur.getColumnIndex(CNT_NAME)),
cur.getString(cur.getColumnIndex(CNT_NUMBER)),
cur.getBlob(cur.getColumnIndex(CNT_PHOTO))
});
} while (cur.moveToNext());
}
cur.close();
return mMatrixCursor;
}
HomeActivity.java
在該功能我填寫我的自定義的ListView。
public void FillListView() {
SimpleCursorAdapter adap;
// Getting reference to listview
ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
DBhelper dh = new DBhelper(this);
dh.open();
MatrixCursor Mcursor = dh.retriveCntDetails();
dh.close();
adap = new SimpleCursorAdapter(getBaseContext(),
R.layout.lv_layout,
null,
new String[]{"name", "number", "photo"},
new int[]{R.id.tv_name, R.id.tv_number, R.id.iv_photo}, 0);
// Setting the adapter to listview
lstContacts.setAdapter(adap);
// Setting the cursor containing contacts to listview
adap.swapCursor(Mcursor);
}
請看看'SimpleCursorAdapter'源... defualt ImageViews(在行佈局)只能加載URL(如果列是字符串)或加載資源的id(如果列是int) ...你需要使用自定義的'SimpleCursorAdapter.ViewBinder'(這很簡單,你應該通過簡單的谷歌搜索找到一些資源)...... **編輯:**爲什麼你使用'MatrixCursor'而不是光標'mDb。 rawQuery'返回? – Selvin
@Selvin謝謝你的建議,我會試着找到一些東西。 – Azazel
'new SimpleCursorAdapter.ViewBinder(){@Override public boolean setViewValue(View view,Cursor cursor,int columnIndex){if(view.getId()== R.id.iv_photo){byte [] data = cursor.getBlob( columnIndex);/*然後從「數據」解碼位圖(在不同的線程上執行此操作會很好......但爲了測試原因,您可以嘗試不使用它),將「視圖」投射到ImageView並將位圖設置爲它* /返回true ; } return false;/*其他視圖使用默認實現* /}' – Selvin