0

我有一个活动,我正在尝试使用用户提供的数据保存联系人。我发现返回的 Uri 为空......我已经测试了一些代码,而在其他地方不允许空值,它可以工作......但是在这里它似乎不起作用。相反,它给了我一个 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这是我使用 Android ContactsContract API 通过使用,ContentProviderResult和方法应用批处理操作来保存联系人的代码。我applyBatch的目标是返回一个 Uri,然后将使用它来使用 Intent 来查看联系人卡片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;
} 
4

1 回答 1

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");
                }
            }
        }    
于 2013-11-13T07:43:26.860 回答