0

I am made an android application which get contacts from vcf file and save to android device and the code is:

String tmptype = mime.getMimeTypeFromExtension("vcf");
final File file = new File(Environment.getExternalStorageDirectory().toString() + "/contacts.vcf");
Intent i = new Intent();
i.setDataAndType(Uri.fromFile(file), "text/x-vcard");
startActivity(i);

It saves all contacts into device but problem is it wants userinterface to choose where to save.But I want directly save to PhoneMemory or Phonebook.I don't want any other option while inserting Contacts.I have search on web could not find any answer please help to find out this

JOHNCENA
  • 99
  • 1
  • 7

1 Answers1

0
public class VCardActivity extends Activity {
Cursor cursor;
ArrayList<String> vCard;
String vfile;
static Context mContext;

/** Called when the activity is first created. */
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mContext = VCardActivity.this;
        getVCF();
    }

    public static 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();
                Log.d("Vcard", VCard);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
}

Add on Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Credit to: https://stackoverflow.com/a/12048639/2378691

Community
  • 1
  • 1
Ido Naveh
  • 2,442
  • 3
  • 26
  • 57
  • this code works in case of if we want save contact from phone to sdcard as vcf.I need help for vice versa of this process with out userinterface – JOHNCENA Dec 22 '14 at 04:21