android_备份还原通讯录

package cn.m15.helper.logic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.ContactsContract;

public class ContactsSyncer extends Syncer {

	public void backup(Context context) {
		ContentResolver cr = context.getContentResolver();
		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
				null, null, null);
		int index = cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
		FileOutputStream fout;
		try {
			fout = new FileOutputStream(
					Environment.getExternalStorageDirectory() + "/go.vcf");
			byte[] data = new byte[1024 * 1];
			while (cur.moveToNext()) {
				String lookupKey = cur.getString(index);
				Uri uri = Uri.withAppendedPath(
						ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
				AssetFileDescriptor fd = context.getContentResolver()
						.openAssetFileDescriptor(uri, "r");
				FileInputStream fin = fd.createInputStream();
				int len = -1;
				while ((len = fin.read(data)) != -1) {
					fout.write(data, 0, len);
				}
				fin.close();
			}
			fout.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void restore(Context context, String filename) {
		Intent intent = new Intent();
		intent.setPackage("com.android.contacts");
		Uri uri = Uri.fromFile(new File(filename));
		intent.setAction(Intent.ACTION_VIEW);
		intent.setDataAndType(uri, "text/x-vcard");
		context.startActivity(intent);
	}
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。