gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:00:55 24 4
gpt4 key购买 nike

我想做的是生成一个.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com