2012-03-31 3 views
0

현재 SQLiteDatabase에 저장된 연락처 목록을 표시하려고합니다. 이전에 ContactsContract.Contacts.PHOTO_THUMBNAIL_URI을 검색하여 데이터베이스 행의 BLOB 열에 byte[]의 형식으로 저장했습니다.ViewBinder가있는 SimpleCursorAdapter로 SQLite BLOB 열

지금 내가 MyBinderView 클래스 Bitmaps로 디코딩하여, 다시 썸네일을 추출하기 위해 노력하고있을 때, 그림이 표시되지 않습니다, 대신 나는 빈 공간 (기본 이미지, ic_launcher 제대로 보여됩니다)를 참조하십시오. 내 ListView 행 레이아웃 :

<?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 클래스 : 사진에 대한

//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 클래스가 올바르게 삽입되는 :

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


} 

어떤 도움을 주시면 감사하겠습니다.

답변

0

ContactsContract.Contacts.PHOTO_THUMBNAIL_URI

를 검색 할 수 있습니다 썸네일에 대한 경로를 제공합니다. 따라서이 경로를 사용하여 URI을 빌드하면 parse 함수를 호출하는 것으로 이해해야합니다.

다음이 포함 된 클래스의 도움으로 새로운 URI를 조회 -

private static class PhotoQuery { 
    public static final String[] PROJECTION = { 
     Photo.PHOTO 
    }; 

    public static final int PHOTO = 0; 
} 

당신이 문제를 해결 필요한 바이트 []를 추출 할 수 있습니다 노호 코드를 사용하여.

바이트를 가져 오는 지점 []은 DB에 저장하고 필요할 때 나중에 조작 할 수 있습니다.

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

는 사람 환호 도움이됩니다 희망 :)