一个月前我问了一个类似的问题,但没有得到真正有帮助的答案,所以我想看看我是否可以更好地表达这个问题。
我正在使用将 BLOB 数据存储在数据库中的应用程序。数据是图片图像,非常小(小于 2KB)。由于大小,我决定将它们直接放入数据库中。
我还在学习编程,我确定我错过了一些简单的东西,但这是我的问题。我的数据库中有很多文本字段,我使用以下方法从它们中提取信息:
private void fillData() {
// Pulls the fields from the database and assigns them to the columns
String[] from = new String[] { BorrowMeTable.COLUMN_ITEM,
BorrowMeTable.COLUMN_NAME, BorrowMeTable.COLUMN_DATE };
// The label for each textview
int[] to = new int[] { R.id.label, R.id.pName, R.id.pDate };
getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.bm_row, null, from,
to, 0);
setListAdapter(adapter);
}
我的类实现了 LoaderManager.LoaderCallbacks,附带的方法如下:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Remove to show all records - This sets up the list to filter based on
// items returned
String mSelection = BorrowMeTable.COLUMN_BORROW_FLAG + "=?";
// SQL request for only returned items to be shown
String[] selectionArgs = { "Borrowed" }; // The actual argument
String[] projection = { BorrowMeTable.COLUMN_ID,
BorrowMeTable.COLUMN_ITEM, BorrowMeTable.COLUMN_NAME,
BorrowMeTable.COLUMN_DATE };
// mSelection & selectionArgs can be removed and set to null to display
// all records
CursorLoader cursorLoader = new CursorLoader(this,
BorrowMeContentProvider.CONTENT_URI, projection, mSelection,
selectionArgs, null);
return cursorLoader;
}
当我尝试添加其中包含 blob 数据的列时 - 我收到一条错误消息,提示我无法将 blob 转换为字符串:
06-30 15:13:36.152: E/AndroidRuntime(842): android.database.sqlite.SQLiteException: unknown error (code 0): Unable to convert BLOB to string
我知道这是因为我试图像其他所有内容一样将它作为字符串读取,但我不知道如何使用这种方法正确抓取它。我正在将数据加载到列表活动的行中。
谁能帮我指出正确的方向?我没有在网上找到很多处理光标加载器和列表视图的内容。
谢谢
更新 - 我为简单的光标适配器创建了一个新的绑定视图,但我仍然收到以下错误消息:
07-02 20:42:53.350: E/AndroidRuntime(798): FATAL EXCEPTION: main
07-02 20:42:53.350: E/AndroidRuntime(798): android.database.sqlite.SQLiteException: unknown error (code 0): Unable to convert BLOB to string
07-02 20:42:53.350: E/AndroidRuntime(798): at android.database.CursorWindow.nativeGetString(Native Method)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.database.CursorWindow.getString(CursorWindow.java:434)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:150)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.CursorAdapter.getView(CursorAdapter.java:250)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.ListView.onMeasure(ListView.java:1158)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.View.measure(View.java:15518)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.View.measure(View.java:15518)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.View.measure(View.java:15518)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.View.measure(View.java:15518)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-02 20:42:53.350: E/AndroidRuntime(798): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.View.measure(View.java:15518)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.os.Handler.handleCallback(Handler.java:725)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.os.Handler.dispatchMessage(Handler.java:92)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.os.Looper.loop(Looper.java:137)
07-02 20:42:53.350: E/AndroidRuntime(798): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-02 20:42:53.350: E/AndroidRuntime(798): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 20:42:53.350: E/AndroidRuntime(798): at java.lang.reflect.Method.invoke(Method.java:511)
07-02 20:42:53.350: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-02 20:42:53.350: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-02 20:42:53.350: E/AndroidRuntime(798): at dalvik.system.NativeStart.main(Native Method)
这是我用于视图活页夹的代码:
public class MyViewBinder implements ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
// TODO Auto-generated method stub
int viewId = view.getId();
Log.i("ViewBinder: view", Integer.toString(viewId));
Log.i("ViewBinder: name",cursor.getString(2));
Log.i("ViewBinder: email",cursor.getString(3));
Log.i("ViewBinder: photo",cursor.getBlob(4)==null?"NO Photo":"Has photo");
switch(viewId){
case R.id.pic_of_item:
ImageView storedItem = (ImageView) view;
byte[] blob = cursor.getBlob(columnIndex);
// If the blob is null then pull it from database
if (blob != null){
// Decode the bitmap from the blob
storedItem.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0, blob.length));
Log.i("RETURN TRUE BLOB",cursor.getString(3));
} else {
// If nothing is found use the camera icon from drawable
storedItem.setImageResource(R.drawable.take_camera_small);
Log.i("RETURN TRUE NO BLOB",cursor.getString(3));
return true;
}
}
Log.i("RETURN FALSE",cursor.getString(3));
return false;
}
}