我要做的是为SD卡生成一个.vcf文件!但我得到了这个例外。有人可以帮我吗。
我使用的代码如下所示,我不知道为什么会给出
java.io.IOException:读取失败:EINVAL(无效参数)
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();
}
}
}
答案 0 :(得分:1)
使用此方法创建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;
}