标签:des android os io java ar art cti log
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); } }
标签:des android os io java ar art cti log
原文地址:http://blog.csdn.net/zhpforever/article/details/39004107