在 android 2.1 中运行良好
代码
String vfile ="POContactsRestore.vcf";
String VCard = null;
File vfile = new File("/sdcard/POContactsRestore.vcf");
if(vfile.exists()){
vfile.delete();
}
FileOutputStream mFileOutputStream = null;
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);
phones.moveToFirst();
for(int i =0;i<phones.getCount();i++)
{
String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd;
try
{
fd = getContentResolver().openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
VCard = new String(buf);
String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
mFileOutputStream = new FileOutputStream(path, false);
phones.moveToNext();
Log.d("Vcard", VCard);
}
catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
mFileOutputStream.write(VCard.toString().getBytes());
mFileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
日志错误
02-14 06:03:26.724: E/AndroidRuntime(862): java.lang.NullPointerException
02-14 06:03:26.724: E/AndroidRuntime(862): at com.example.com.example.contactbackup.Contact$1.onClick(Contact.java:148)
02-14 06:55:38.183: E/AndroidRuntime(1066): FATAL EXCEPTION: main
02-14 06:55:38.183: E/AndroidRuntime(1066): java.lang.NullPointerException
02-14 06:55:38.183: E/AndroidRuntime(1066): at com.example.com.anthem.contactbackup.Contact$1.onClick(Contact.java:101)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.view.View.performClick(View.java:4084)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.view.View$PerformClick.run(View.java:16966)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.os.Handler.handleCallback(Handler.java:615)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.os.Looper.loop(Looper.java:137)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-14 06:55:38.183: E/AndroidRuntime(1066): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 06:55:38.183: E/AndroidRuntime(1066): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 06:55:38.183: E/AndroidRuntime(1066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-14 06:55:38.183: E/AndroidRuntime(1066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-14 06:55:38.183: E/AndroidRuntime(1066): at dalvik.system.NativeStart.main(Native Method)