这是我为将照片作为 vCard 的一部分发送的操作...需要考虑的重要事项:
1)设备:运行Android 6.01的中兴Axon 7
2) 无法让 vCard 3.0 或 4.0 正常运行,只能使用 vCard 2.1
File vcfFile = new File(DisplayContactActivity.this.getExternalFilesDir(null), "generated.vcf");
try
{
/**Only Version 2.1 worked for me with or without PHOTO**/
FileWriter fw = new FileWriter(vcfFile);
fw.write("BEGIN:VCARD\r\n");
fw.write("VERSION:2.1\r\n");
fw.write("FN:" + "Contact 7" + "\r\n");
/*Getting the name of the File as I had saved it*/
String file_name = ("current_contact_image" + CONTACT_ID);
/*Get the bitmap that we stored in a File*/
Bitmap bitmap = getContactImage(file_name);
/*Convert bitmap to Base64*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String image_encoded = Base64.encodeToString(b, Base64.DEFAULT);
/*Write the encoded version of image to vCard 2.1, NOTICE that no determining whether the image is GIF or JPEG is needed*/
fw.write("PHOTO;ENCODING=BASE64:" + image_encoded + "\r\n");
/*Write some other stuff to the vCard also just trying to give whoever needs this a starting point*/
fw.write("TEL;Primary:" + "(586) 268-3437" + "\r\n");
fw.write("TEL;OTHER:" + "(313) 313-4545" + "\r\n");
fw.write("ADR;OTHER:" + "12345 AnyLane Dr." + "\r\n");
fw.write("ADR;OTHER:" + "54321 AnyPlace Av." + "\r\n");
fw.write("EMAIL;OTHER:" + "email@yahoo.com" + "\r\n");
fw.write("EMAIL;OTHER:" + "email@wowway.com" + "\r\n");
fw.write("END:VCARD\r\n");
fw.close();
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(vcfFile), "text/x-vcard");
startActivity(i);
}
catch (Exception e)
{
Log.i(TAG, "Exception: " + e);
}
public Bitmap getContactImage(String file_name)
{
Log.i(TAG, "Running getContactImage() with file name: " + file_name);
Bitmap thumbnail = null;
//------------------------------------------------------------------------------------------
try
{
File filePath = getBaseContext().getFileStreamPath(file_name);
FileInputStream fis = new FileInputStream(filePath);
thumbnail = BitmapFactory.decodeStream(fis);
}
catch (Exception e) //Use scaled_bitmap_for_storage instead of the current_contact_image file name
{
/*Error getting user-selected image, set boolean to get default image*/
Log.i(TAG, "Exception while running getContactImage() for file name: " + file_name + " with error message: " + e);
}
//------------------------------------------------------------------------------------------
return thumbnail;
}