3

電話のすべての連絡先を .vcf ファイル (vCard) として SD カードに保存しようとしています。動作しますが、問題があります。複数の電話番号 (携帯電話番号と勤務先番号) を持つすべての連絡先は、2 回保存されます。両方の番号がそれぞれの重複する連絡先に含まれているため、正しい、重複しているだけです。誰かがこの問題を解決する方法を教えてもらえますか? 私のコードは次のとおりです。

File delete=new File(Environment.getExternalStorageDirectory()+"/Contacts.vcf");

     if (delete.exists()) {
       delete.delete();

     }

    Cursor phones = ContactService.this.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 = ContactService.this.getContentResolver().openAssetFileDescriptor(uri, "r");
            FileInputStream fis = fd.createInputStream();
            byte[] buf = new byte[(int) fd.getDeclaredLength()];
            fis.read(buf);
            String VCard = new String(buf);
            String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
            FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
                       mFileOutputStream.write(VCard.toString().getBytes());           
            phones.moveToNext();             
        } 
        catch (Exception e1) 
        {
             // TODO Auto-generated catch block
             e1.printStackTrace();
        }


      }

ご協力ありがとうございました。

4

2 に答える 2

4

以下のコードを使用して、連絡先データを vcf ファイルとしてデバイスの SD カードに保存します。

public class VCardActivity extends Activity {
    Cursor cursor;
    ArrayList<String> vCard;
    String vfile;
    static Context mContext;

    /** Called when the activity is first created. */
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mContext = VCardActivity.this;
            getVCF();
        }

        public static void getVCF() {
            final String vfile = "Contacts.vcf";
            Cursor phones = mContext.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 = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
                    FileInputStream fis = fd.createInputStream();
                    byte[] buf = new byte[(int) fd.getDeclaredLength()];
                    fis.read(buf);
                    String VCard = new String(buf);
                    String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
                    FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
                    mFileOutputStream.write(VCard.toString().getBytes());
                    phones.moveToNext();
                    Log.d("Vcard", VCard);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    }

詳細については、以下のリンクを参照してください。

連絡先を VCF ファイルとしてエクスポート

ファイルに以下の許可を与えandroidmanifest.xmlます。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-08-21T04:57:51.997 に答える
2

lookupKeyの重複を確認してください...

于 2012-08-28T11:00:14.767 に答える