如何提高联系人插入性能?

时间:2014-12-20 18:50:25

标签: android android-contentprovider android-contacts contactscontract query-performance

我正在使用applyBatch方法为其插入Android应用程序,我的内容解析器显示空指针异常。

public void import_contacts(File paramFile, int paramInt)
            throws IOException {
        String[] arrayOfString;
        i1 = 0;
        try {
            FileInputStream localFileInputStream = new FileInputStream(
                    paramFile);
            BufferedReader localBufferedReader = new BufferedReader(
                    new InputStreamReader(localFileInputStream));
            for (;;) {
                String str = localBufferedReader.readLine();
                if (str == null) {
                    localBufferedReader.close();
                    mProgress.setProgress(100);
                    mHandler.post(new Runnable() {
                        public void run() {
                            alertdialog_Contact(totalRecToImport
                                    + " Contacts were imported Successfully");
                        }
                    });
                    return;
                }
                i1++;
                float f = i1;
                mProgressStatus = ((int) (f / (paramInt / 100)));
                mHandler.post(new Runnable() {
                    public void run() {
                        mProgress.setProgress(mProgressStatus);
                    }
                });
                Log.d("TAG", str);
                arrayOfString = str.split(";");
                if (arrayOfString.length >= 4) {
                    // if (arrayOfString.length != 4) {
                    // break;
                    // }
//                  insertLine(arrayOfString[0], "", arrayOfString[3], "", "",
//                          "", "", "", "", "", "", "");
                    op_list = new ArrayList<ContentProviderOperation>();

                    op_list.add(ContentProviderOperation
                            .newInsert(RawContacts.CONTENT_URI)
                            .withValue(RawContacts.ACCOUNT_TYPE, null)
                            .withValue(RawContacts.ACCOUNT_NAME, null).build());
                    op_list.add(ContentProviderOperation
                            .newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValue(
                                    ContactsContract.Data.MIMETYPE,
                                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                            .withValue(
                                    ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
                                    arrayOfString[0]).build());

                    op_list.add(ContentProviderOperation
                            .newInsert(Data.CONTENT_URI)
                            .withValue(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                            .withValue(Phone.NUMBER, arrayOfString[3])
                            .withValue(Phone.TYPE, Phone.TYPE_MOBILE)
                            .withValue(Phone.TYPE, Phone.TYPE_WORK).build());
                    applytobatch++;
                }
                if(applytobatch == 100){
                    applytobatch=0;
                    try {
                           getContentResolver().
                              applyBatch(ContactsContract.AUTHORITY, op_list);
                        } catch (RemoteException e) {
                        } catch (OperationApplicationException e) {
                        }
                }
            }
        } catch (FileNotFoundException localFileNotFoundException) {
            for (;;) {
                Toast.makeText(this, localFileNotFoundException.getMessage(), 0)
                        .show();
                localFileNotFoundException.printStackTrace();
            }
        }
    }

错误:

12-20 23:59:21.268: E/AndroidRuntime(3151): FATAL EXCEPTION: Thread-18
12-20 23:59:21.268: E/AndroidRuntime(3151): java.lang.NullPointerException
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.os.Parcel.readException(Parcel.java:1328)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:137)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:449)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:95)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.content.ContentResolver.applyBatch(ContentResolver.java:639)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at com.example.contact_export.MainActivity.import_contacts(MainActivity.java:385)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at com.example.contact_export.MainActivity$4.run(MainActivity.java:294)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at java.lang.Thread.run(Thread.java:1019)

我正在尝试插入10000个联系人,请告诉我必须遵循哪种方法?

1 个答案:

答案 0 :(得分:0)

您无法插入具有空帐户名和帐户类型的联系人。同时保持批量大小最多为50.帐户名称和类型应为可写帐户,或者您应在设备中创建自己的帐户。