如何备份/恢复的android联系人编程? [英] How to backup/restore a contacts in android programmatically?

查看:143
本文介绍了如何备份/恢复的android联系人编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好 sackoverflow 我想开发一个应用程序,它可以备份和恢复通讯录,我的code是如下:

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

在清单权限

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

到目前为止,我的code的备份联系人,但只有姓名和联系号码,但没有像检索的联系人是否出演与否的信息。请帮我解决这个谜。

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.

先谢谢了。

推荐答案

使用此恢复:

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

这篇关于如何备份/恢复的android联系人编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆