如何以编程方式备份​​/恢复android中的联系人?

时间:2013-09-30 04:20:43

标签: android android-contacts

您好sackoverflow我正在尝试开发一个可以备份和恢复联系人的应用程序,我的代码如下

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();
    }
}
}

清单中的权限

<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"/>

到目前为止,我的代码正在备份联系人,但仅提供姓名和联系电话,但不检索联系人是否加星标等信息。请帮助我解决这个谜题。

提前致谢。

2 个答案:

答案 0 :(得分:1)

使用此功能恢复:

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);

答案 1 :(得分:0)

Intent mIntent =新的Intent(Intent.ACTION_VIEW); mIntent.setDataAndType(Uri.fromFile(new File(filePath)),MimeTypeMap.getSingleton()。getMimeTypeFromExtension(“ vcf”));; startActivity(Intent.createChooser(mIntent,“ Select App”));

相关问题