我有一个Activity,我试图使用用户提供的数据来保存联系人。我发现返回的Uri是null ...我已经测试了一些代码而没有在其他地方允许空值而且它有效。 ..但这里它似乎没有工作。相反它给了我一个NullPointerException。
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.blutechnologies.scancard.BusinessCard$2.onClick(BusinessCard.java:195)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
这是我用来通过使用ContentProviderOperation
,ContentProviderResult
和applyBatch
方法应用批处理操作来使用Android ContactsContract API保存联系人的代码。我的目的是返回一个Uri,然后将使用ACTION_VIEW
的意图来查看联系人卡片。
btn_save.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
Uri uri=saveContacts(sname.getText().toString(), sphone.getText().toString(), sphone2.getText().toString(), semail.getText().toString(), semail2.getText().toString(), sweb.getText().toString(), sweb2.getText().toString(), pathName);
Log.d(TAG, "Contact saved with uri "+uri.toString());
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}});
}
public Uri saveContacts(String name,String mobile,String home_phone,String work_email,String personal_email,String website,String website2,String pathName)
{
ContentProviderOperation operation=null;
ContentProviderResult[] res = null;
ArrayList<ContentProviderOperation> ops=new ArrayList<ContentProviderOperation>();
Uri uri=null;
int rawContactId=ops.size();
operation=ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build();
ops.add(operation);
if(name!=null)
{
operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId).withValue(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE).withValue(StructuredName.DISPLAY_NAME,name).build();
ops.add(operation);
}
if(mobile!=null)
{
operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId).withValue(ContactsContract.Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE).withValue(Phone.NUMBER,mobile).withValue(Phone.TYPE,Phone.TYPE_MOBILE).build();
ops.add(operation);
}
if(home_phone!=null)
{
operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId).withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE).withValue(Phone.NUMBER, home_phone).withValue(Phone.TYPE, Phone.TYPE_HOME).build();
ops.add(operation);
}
if(work_email!=null)
{
operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId).withValue(ContactsContract.Data.MIMETYPE,Email.CONTENT_ITEM_TYPE).withValue(Email.ADDRESS,work_email).withValue(Email.TYPE,Email.TYPE_WORK).build();
ops.add(operation);
}
if(personal_email!=null)
{
operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactId).withValue(ContactsContract.Data.MIMETYPE,Email.CONTENT_ITEM_TYPE).withValue(Email.ADDRESS,personal_email).withValue(Email.TYPE,Email.TYPE_HOME).build();
ops.add(operation);
}
if(website!=null)
{
operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactId).withValue(ContactsContract.Data.MIMETYPE,Website.CONTENT_ITEM_TYPE).withValue(Website.URL,website).withValue(Website.TYPE,Website.TYPE_WORK).build();
ops.add(operation);
}
if(website2!=null)
{
operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactId).withValue(ContactsContract.Data.MIMETYPE,Website.CONTENT_ITEM_TYPE).withValue(Website.URL,website2).withValue(Website.TYPE,Website.TYPE_HOME).build();
ops.add(operation);
}
if(pathName!=null)
{
byte[] imgArray=pathName.getBytes();//Error is here...found it.
operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactId).withValue(ContactsContract.Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE).withValue(Photo.PHOTO,imgArray).build();
ops.add(operation);
}
try {
res=getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
if(res!=null && res.length>=1)
{
uri=res[0].uri;
Log.d(TAG,"Contact saved successfully");
}
} catch (RemoteException e) {
Log.d(TAG,"Error performing batch operation to insert contact RemoteException",e);
} catch (OperationApplicationException e) {
Log.d(TAG,"Error performing batch operations to insert contact OperationApplicationException",e);
}
return uri;
}
答案 0 :(得分:0)
我发现了错误,代码以某种方式突变到了使用以下方式获取将byte[]
保存到ContactsContract.Data
表所需的byte[] imgArray=pathName.getAllBytes()
FileInputStream fos=null;
try {
File file=new File(pathName);
byte[] buffer=new byte[(int)file.length()];
fos=new FileInputStream(file);
if(fos.read(buffer)!=-1)
{
Log.d(TAG, "File has been read to byte buffer");
operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactId).withValue(ContactsContract.Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE).withValue(Photo.PHOTO,buffer).build();
ops.add(operation);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.e(TAG,"File could not found",e);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Error when performing IO operation",e);
}
finally
{
if(fos!=null)
{
try {
fos.close();
} catch (IOException e) {
Log.e(TAG, "Error closing the inputStream while cleaning up");
}
}
}
我改变了代码说:
{{1}}