export contacts from listview of my application - android

i want to copy just the checked contacts to sd card as a vcf file
Is there any way to copy the checked contacts from a list view to s d card ? i have made an app in which i can export contacts from the contacts menu but i want to export contacts from a list view and which are checked contacts.please kindly help. i have to submit a project.
itemsSelected = "Selected items: \n";
for (int i = 0; i < lstContacts.getCount(); i++) {
view = lstContacts.getChildAt(i);
check= (CheckBox)view.findViewById(R.id.cb2);
if (check.isChecked()) {
itemsSelected += lstContacts.getItemAtPosition(i) + "\n";
}
}

Finally i got the answer to this question.
in this,lv is the contactlist from listview. just copy this code and you can check the contacts which you want to copy to the sd card.
long [] ids = lv.getCheckedItemIds();
for(long id : ids) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, 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 = this.getContentResolver().openAssetFileDescriptor(uri, "r");
fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String VCard = new String(buf);
//private File mPath = new File(Environment.getExternalStorageDirectory() + "//yourdir//");
sdCard =new File(Environment.getExternalStorageDirectory() + "//yourdir//");
directory = new File (sdCard.getAbsolutePath());
directory.mkdirs();
file = new File(directory, vfile);
// String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
FileOutputStream mFileOutputStream = new FileOutputStream(file,true);
mFileOutputStream.write(VCard.toString().getBytes());
phones.moveToNext();
//Log.d("Vcard", VCard);
}
catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}}
Toast.makeText(this, "contacts copied to " + vfile,
Toast.LENGTH_SHORT).show();

Related

Get contacts and send to server as a vcf file in android

I am trying to convert my contacts to .vcf file in android but it gives an error and can not export contacts.
It gives me an error it goes in the catch block and gives:
java.lang.NegativeArraySizeException: -1
I want to export my all contacts to server using vcf file
public void getVCF() {
final String vfile = "Contacts.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");
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();
filevcf = new File(path);
Log.d("Vcard", VCard);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}

How can I create .vcf file for a single contact?

I have successfully created .vcf file for the all contacts from my android device. I have referred below link for doing so :
Export the Contacts as VCF file
and its working quite good.
But, I need to convert a single contact to single .vcf file as per my requirement.
In this current code getting all the contacts using below lines :
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
and then all contacts will be covered in .vcf file.
So, What is way that I can generate .vcf file for a single contact ?
See the first line in the below code block. Here I am creating a new vFile everytime, this method will be called.So each contact will be saved in different files.
public void get(Cursor cursor)
{
vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf";
//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");
// Your Complex Code and you used function without loop so how can you get all Contacts Vcard.??
/* 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 out = new FileOutputStream(path);
out.write(VCard.toString().getBytes());
Log.d("Vcard", VCard);*/
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, false);
mFileOutputStream.write(vcardstring.toString().getBytes());
} catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

How to store .vcf file in separate folder in internal SDCard

I am using following code to store vcf file that i am generating from code
I am able to generate vcf file
but it appears under root directory in SDcard
I want to store it under seperate folder
How do i meet to requirements? Plz help
Thanks you.
private void getVcardString() throws IOException {
final String vfile = "BackupCont" + currDate + ".vcf";
vCard = new ArrayList<String>();
cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
Cursor c = dbhandle.getContactsByIsChecked();
ArrayList<String> arrchk=new ArrayList<String>();
for(int cc=0;cc<c.getCount();cc++)
{
arrchk.add(c.getString(c.getColumnIndex("con_name")));
}
if (cursor != null && cursor.getCount() > 0) {
int i;
String new_path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/Folder_name";
// String path = "c:/";
File dir = new File(new_path);
if (!dir.exists())
dir.mkdirs();
String storage_path = Environment.getExternalStorageDirectory()+"/Folder_name"+vfile;
Toast.makeText(getApplicationContext(), storage_path,Toast.LENGTH_LONG).show();
FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
cursor.moveToFirst();
for (i = 0; i < cursor.getCount(); i++)
{
if(arrchk.contains(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))))
{
get(cursor);
cursor.moveToNext();
mFileOutputStream.write(vCard.get(i).toString().getBytes());
}
}
mFileOutputStream.close();
cursor.close();
Toast.makeText(getApplicationContext(), "Created...",Toast.LENGTH_LONG).show();
Uri uri=Uri.fromFile(new File(storage_path,""+vfile));
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("application/x-vcard");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
else
{
Log.d("TAG", "No Contacts in Your Phone");
}
}
String outputDirectory = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "subFolderName";
outputDirectory.mkdirs();
File outputFile = new File(outputDirectory, filename);
FileOutputStream fos = new FileOutputStream(outputFile);
You should also check Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) to ensure that the external storage is in fact available.

Prevent adding duplicate contacts in a vcf file

I have copied the code from the below link to export contacts to a vcf file.it works fine but the problem is Every contact that has more than one phone number (a mobile and work number) are saved twice. And both of the numbers are in each duplicate contact, so they are getting duplicated. please help
Export the Contacts as VCF file
Cursor phones = 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 = this.getContentResolver().openAssetFileDescriptor(uri, "r");
fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String VCard = new String(buf);
//private File mPath = new File(Environment.getExternalStorageDirectory() + "//yourdir//");
sdCard =new File(Environment.getExternalStorageDirectory() + "//yourdir//");
directory = new File (sdCard.getAbsolutePath());
directory.mkdirs();
file = new File(directory, vfile);
// String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
FileOutputStream mFileOutputStream = new FileOutputStream(file,true);
mFileOutputStream.write(VCard.toString().getBytes());
phones.moveToNext();
//Log.d("Vcard", VCard);
}
catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}}
Toast.makeText(this, "contacts copied to " + vfile,
Toast.LENGTH_SHORT).show();

How to import or insert contacts from .vcf file in Android programmatically?

I create .vcf file of all contacts in Android using below code.
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");
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();
}
}
}
How to add / restore contacts from .vcf file programatically in Android?
I want to restore all contacts form that .vcf file in to Android using code.
How to do this?
Actually I also want to do the same and a after a long research finally i got the solution.
Here is the piece of code to restore:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(storage_path+vfile)),"text/x-vcard"); //storage path is path of your vcf file and vFile is name of that file.
startActivity(intent);
Enjoy!!

Categories

Resources