尝试读取vCard联系人数据时Android读取失败异常

时间:2012-08-23 18:12:20

标签: android ioexception vcard fileoutputstream ioerror

我正在尝试使用以下代码将联系人导出为vCard格式:

(在此处找到:http://androidcodeexamples.blogspot.in/2012/06/export-contacts-as-vcf-file-in-android.html

Cursor phones = mContext.getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                              null, null, null);
        phones.moveToFirst();
        for (int i = 0; i < phones.getCount(); i++) {
               String lookupKey = phones.getString(phones
                       .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
               Uri uri = Uri.withAppendedPath(
               ContactsContract.Contacts.CONTENT_VCARD_URI,
                                             lookupKey);
               AssetFileDescriptor fd;
               try {
                       fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
                       FileInputStream fis = fd.createInputStream();
                       byte[] buf = new byte[(int) fd.getDeclaredLength()];
                       fis.read(buf);
                       String VCard = new String(buf);
                       String path = Environment.getExternalStorageDirectory()
                                .toString() + File.separator + vfile;
                       FileOutputStream mFileOutputStream = new FileOutputStream(path,
                                 true);
                       mFileOutputStream.write(VCard.toString().getBytes());
                       phones.moveToNext();
                       Log.d("Vcard", VCard);
               } catch (Exception e1) {
                       // TODO Auto-generated catch block
                       e1.printStackTrace();
               }
        }

但是我觉得fis.read(buf)引起了异常。

例外是:“读取失败:EINVAL(无效参数)。”

有人能帮助我吗?

谢谢! :)

4 个答案:

答案 0 :(得分:1)

我刚看到相同的行为(尝试使用Android 4.0.3和4.1.2的模拟器)。它在真实设备上工作正常,不适用于模拟器。

答案 1 :(得分:1)

package com.example.temp;

  import java.io.File;
  import java.io.FileDescriptor;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.InputStream;
  import java.util.ArrayList;

  import android.app.Activity;
  import android.content.Context;
  import android.content.res.AssetFileDescriptor;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.Environment;
  import android.provider.ContactsContract;
  import android.util.Log;

  public class MainActivity extends Activity {
Cursor cursor;
ArrayList<String> vCard;
String vfile;
static Context mContext;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        getVCF();
    }

    public static void getVCF() {
        final String vfile = "POContactsRestore.vcf";
        Cursor phones = mContext.getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        phones.moveToFirst();
        for (int i = 0; i < phones.getCount(); i++) {
            String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
            AssetFileDescriptor fd;
            try {
                fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
                FileDescriptor fdd = fd.getFileDescriptor();
                InputStream in = new FileInputStream(fdd);
                //FileInputStream fis = fd.createInputStream();
                Log.d("BLA BLA", "Len is :: " + (int) fd.getDeclaredLength());
                byte[] buf = new byte[(int) fd.getDeclaredLength()];
                in.read(buf);
                fd.close();
                String VCard = new String(buf);
                String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
                FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
                mFileOutputStream.write(VCard.toString().getBytes());
                mFileOutputStream.close();
                in.close();
                phones.moveToNext();
                Log.d("Vcard", VCard);

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
}

答案 2 :(得分:0)

如果AssetFileDescriptor未按长度初始化,则

getDeclaredLength返回-1 在这种情况下,最好使用getStatSize()

答案 3 :(得分:0)

我有一个相同的例外,但在一个真实的设备上。 问题是一个电子邮件具有未定义电子邮件类型的联系人