How to backup/restore a contacts in android programmatically?

谁说我不能喝 提交于 2020-01-13 06:27:08

问题


Hello sackoverflow I'm trying to develop an application which can backup and restore contacts, my code is as follows

public class MainActivity extends Activity 
{

Cursor cursor;
ArrayList<String> 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<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");
    }
    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();
    }
}
}

Permissions in manifest

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

So far my code was backing up the contact but only name and contact number, but not retrieving the information like whether the contact is starred or not. Please help me in solving this riddle.

Thanks in advance.


回答1:


Use this to restore:

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



回答2:


Intent mIntent = new Intent(Intent.ACTION_VIEW);
mIntent.setDataAndType(Uri.fromFile(new File(filePath)), MimeTypeMap.getSingleton().getMimeTypeFromExtension("vcf"));
startActivity(Intent.createChooser(mIntent, "Select App"));


来源:https://stackoverflow.com/questions/19086409/how-to-backup-restore-a-contacts-in-android-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!