2012-09-26 34 views
6

我在从我的联系人列表中获取联系人时遇到问题。我使用此代码:在Android中使用文件描述符阅读联系人的信息

final Cursor Contact = cResolver.query(ContactsContract.Contacts.CONTENT_URI, null, 
       ContactsContract.Contacts._ID +" = " + Contact_ID, null,null); 
     Contact.moveToFirst(); 
     String lookupKey = Contact.getString(Contact 
       .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 

     Uri uri = Uri.withAppendedPath(
       ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); 

     AssetFileDescriptor fd = null; 
     FileInputStream fis = null; 

     fd = cResolver.openAssetFileDescriptor(uri, "_ID"); 
     fis = fd.createInputStream(); 
     byte[] buf = new byte[(int) fd.getDeclaredLength()]; 
     fis.read(buf); 
     String vcardstring = new String(buf); 

但我发现了Exception

java.io.IOException: read failed: EINVAL (Invalid argument) 
libcore.io.IoBridge.read(IoBridge.java:432) 

任何一个能帮助我吗?

+0

您可以张贴整个堆栈跟踪? –

+0

嗨AnasBakez,我也面临同样的问题。如果你有解决方案,那么请分享。 –

+0

@abhishekkumargupta如果你仍然没有找到解决方案,解决方案是不使用文件描述符,因为我遇到了一些使用它的问题,一些设备/制造商不使用它,所以我现在得到我想要的内容的所有信息提供者手动。我希望这会有所帮助 – AnasBakez

回答

0

你可以尝试让整条生产线,并将其保存在一个字符串列表,例如:

public List<String> getAnswers(int line) { 
    List<String> answerList = new ArrayList<String>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_GERAL; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    cursor.moveToPosition(line); 

    for(int x=0; x<cursor.getColumnCount();x++){ 
     answerList.add(cursor.getString(x)); 
    } 

    return answerList; 
}