android - 在android中使用文件描述符读取联系人的信息

标签 android contacts file-descriptor

我在从联系人列表中获取联系人时遇到问题。我正在使用这段代码:

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)

谁能帮我解决这个问题?

最佳答案

您可以尝试获取整行并将其保存在字符串列表中,例如:

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

关于android - 在android中使用文件描述符读取联系人的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12602511/

相关文章:

Android dev : PhoneLookup. _ID, Phone.CONTACT_ID, ContactsContract.Contacts._ID 都一样吗?

无法将文件描述符设置为阻塞模式

c - Linux "ls -l"在 C/C++ 中使用文件描述符实现

android - 用于地理定位的后台服务在 Phonegap (android) 中不起作用

php - google api 联系人 php simplexml

Android:共享首选项不保存按钮的状态

qt - 如何安装Qt mobility?

c - 管道在c中是如何工作的?

android - 如何确定 kotlin-multiplatform 项目中的构建类型

android - 我无法在我的一个项目中通过 Gradle 使用 Android 支持库或 Play 服务