Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to restore contacts in android from a .vcf file?
    primarykey
    data
    text
    <p>Hello <code>stackoverflow</code> I'm trying to develop an application that can backup and restore contacts, <code>backup</code> is no problem, but restoring is the problem, here is my code</p> <pre><code>public class MainActivity extends Activity { Cursor cursor; ArrayList&lt;String&gt; vCard ; String vfile; FileOutputStream mFileOutputStream = null; Button btnRestorects = null; Button btnBackupCts = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnRestorects = (Button) findViewById(R.id.buttonRstCts); btnBackupCts = (Button) findViewById(R.id.buttonBackCts); vfile = "contacts.vcf"; final String storage_path = Environment.getExternalStorageDirectory().toString() +"/"+ vfile; final File f = new File(storage_path); btnBackupCts.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { if (!f.exists()) f.createNewFile(); mFileOutputStream = new FileOutputStream(storage_path, false); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } getVcardString(); } }); btnRestorects.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final Intent intent = new Intent(); final MimeTypeMap mime = MimeTypeMap.getSingleton(); String tmptype = mime.getMimeTypeFromExtension("vcf"); final File file = new File(Environment.getExternalStorageDirectory().toString() +"/contacts.vcf"); intent.setDataAndType(Uri.fromFile(file),tmptype); startActivity(intent); } }); } private void getVcardString() { // TODO Auto-generated method stub vCard = new ArrayList&lt;String&gt;(); cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); if(cursor!=null&amp;&amp;cursor.getCount()&gt;0) { cursor.moveToFirst(); for(int i =0;i&lt;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"); } try { mFileOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 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); mFileOutputStream.write(vcardstring.toString().getBytes()); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } </code></pre> <p><code>Permissions</code></p> <pre><code>&lt;uses-permission android:name="android.permission.READ_CONTACTS"/&gt; &lt;uses-permission android:name="android.permission.WRITE_CONTACTS"/&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt; </code></pre> <p>I can restore in the above manner but the problem is when ever the code snippet is invoked I'm getting an alert dialog prompting to choose the application to open file, when I select <code>Contact</code> application it will work, but I want restore to happen automatically when we click the button Please help to learn, and help me in solving this riddle.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload