java - 在android中创建vcf文件时出错

标签 java android

我想做的是生成一个.vcf文件到SD卡!但我得到了这个异常(exception)。有人可以帮我吗。

The exception thrown

我使用的代码如下,我不知道为什么会这样

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

public class Homepage extends ActionBarActivity {
Cursor cursor;
ArrayList<String> vCard ;
String vfile;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homepage);

    @SuppressWarnings("unused")
    final TextView re = (TextView) findViewById(R.id.email_hp);

}


public void createAndBackUp(View view) {

    Thread createVCF = new Thread() {
        public void run() {
            try {
                vfile = "backUpSiv.vcf";
                getVcardString();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    createVCF.start();

}

public void getVcardString() {
    // TODO Auto-generated method stub
    vCard = new ArrayList<String>();
    cursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {

            get(cursor);
            Log.d("TAG",
                    "Contact " + (i + 1) + "VcF String is" + vCard.get(i));
            cursor.moveToNext();
        }

    } else {
        Log.d("TAG", "No Contacts in Your Phone");
    }

}

public void get(Cursor cursor) {

    // cursor.moveToFirst();
    String lookupKey = cursor.getString(cursor
            .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
    Uri uri = Uri.withAppendedPath(
            ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
    AssetFileDescriptor fd;
    try {
        fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");

        FileInputStream fis = fd.createInputStream();
        byte[] buf = new byte[(int) fd.getDeclaredLength()];
        fis.read(buf);
        String vcardstring = new String(buf);
        vCard.add(vcardstring);

        String storage_path = Environment.getExternalStorageDirectory()
                .toString() + File.separator + vfile;
        FileOutputStream mFileOutputStream = new FileOutputStream(
                storage_path, true);
        mFileOutputStream.write(vcardstring.toString().getBytes());

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

最佳答案

使用此方法创建Vcard,

public static String getVcard(String lookupKey, Context context, String filename) {
    final String vfile = filename + ".vcf";

    Uri uri = Uri.withAppendedPath(
            ContactsContract.Contacts.CONTENT_VCARD_URI,
            lookupKey);
    AssetFileDescriptor fd;
    FileOutputStream mFileOutputStream = null;
    try {
        fd = context.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 + "Demo-vcard" + File.separator;

        File file = new File(path);

        if (!file.exists()) {
            file.mkdir();
        }
        path = path + vfile;

        mFileOutputStream = new FileOutputStream(path,
                true);
        mFileOutputStream.write(VCard.toString().getBytes());

        return path;
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } finally {
        if (mFileOutputStream != null) {
            try {
                mFileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return null;
}

关于java - 在android中创建vcf文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27986721/

相关文章:

java - Spring @Transactional 传播属性

java - 什么更快 - 将数据存储在列表中或数据库中? (安卓)

android - 跟踪推送通知并记录它们

android - 删除 Activity 中的顶部蓝色

java - Hibernate 中使用 ByteBuddy 代理的 MethodHandler 陷入无限循环

java - 在哪里可以找到java方法的时间复杂度?

android - 找不到参数multiDexEnabled()的方法[true]

android - Android wearable sdk可以吗

java - 使用比较器对数组进行排序时出现 ClassCastException

java - 无法使用 hibernate 从 oracle 表中选择数据