问题
I'm trying to display a list of contacts that are currently stored in a SQLiteDatabase
.
Previously I've retrieved ContactsContract.Contacts.PHOTO_THUMBNAIL_URI
and stored it in a form of byte[]
in the BLOB
column of my database rows.
Now when I'm trying to extract the thumbnails back, by decoding them to Bitmaps
in MyBinderView
class, the pictures don't appear, instead I see empty spaces(the default image, ic_launcher
, is showed correctly). My ListView
row layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="1dp">
<ImageView
android:id="@+id/thumbnail"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="50dp"
android:layout_marginRight="1dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/email"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"/>
</LinearLayout>
ListFragment
class:
//DataBaseHelper.PHOTO contains a BLOB fetched from sqlite database
//DataBaseHelper.NAME is a String (no problem here)
String[] from = { DataBaseHelper.PHOTO, DataBaseHelper.NAME };
int[] to = new int[] { R.id.thumbnail, R.id.email };
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Give some text to display if there is no data. In a real
// application this would come from a resource.
setEmptyText("No E-mail buddies found");
// We have a menu item to show in action bar.
// setHasOptionsMenu(true);
contacts = new DataBaseHelper(getActivity());
contacts.open();
// Create an empty adapter we will use to display the loaded data.
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.contacts,
null, from, to);
mAdapter.setViewBinder(new MyViewBinder());
setListAdapter(mAdapter);
// Start out with a progress indicator.
setListShown(false);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
ViewBinder
class for the photo to be inserted correctly:
public class MyViewBinder implements ViewBinder{
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.thumbnail:
ImageView picture = (ImageView) view;
byte[] blob = cursor.getBlob(columnIndex);
if(blob!=null){
picture.setImageBitmap(
BitmapFactory.decodeByteArray(blob, 0, blob.length)
);
}
else
picture.setImageResource(R.drawable.ic_launcher);
return true;
}
return false;
}
}
Any help would be appreciated.
回答1:
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI
Provides a path to the thumbnail that can be retrieved by.
So after understanding that you build a URI
using this path by calling parse
function.
next you query your new uri with the help of this embedded class -
private static class PhotoQuery {
public static final String[] PROJECTION = {
Photo.PHOTO
};
public static final int PHOTO = 0;
}
using the code bellow you'll extract the needed byte[] that solved the issue.
the point of getting byte[] is to be able to store it in your DB and manipulate it later on when needed.
private byte[] getImage(String uriString){
if(uriString==null)
return null;
Uri myuri = Uri.parse(uriString);
Cursor photoCursor = getContentResolver().query(myuri, PhotoQuery.PROJECTION, null, null, null);
if (photoCursor != null) {
try {
if (photoCursor.moveToFirst()) {
final byte[] photoBytes = photoCursor.getBlob(PhotoQuery.PHOTO);
if (photoBytes != null) {
return photoBytes;
}
}
} finally {
photoCursor.close();
}
}
return null;
}
Hope it'll help someone cheers :)
来源:https://stackoverflow.com/questions/9951871/sqlite-blob-column-to-simplecursoradapter-with-viewbinder