如何从.vcf文件恢复android中的联系人?

时间:2013-09-27 10:28:50

标签: android android-contacts vcf

您好stackoverflow我正在尝试开发一个可以备份和恢复联系人的应用程序,backup没问题,但恢复是问题,这是我的代码

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

@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;
}

}

Permissions

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

我可以用上面的方式恢复,但问题是什么时候调用代码片段我得到一个警告对话框,提示选择要打开文件的应用程序,当我选择它将起作用的Contact应用程序时,但是我希望当我们点击按钮时自动恢复请帮助学习,并帮助我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

使用此代码恢复联系人

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