3

在 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)
4

1 回答 1

3

试试这个,问题出在 FileInputStream fis = fd.createInputStream();

package com.example.temp;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log;

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

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mContext = MainActivity.this;
            getVCF();
        }

        public static void getVCF() {
            final String vfile = "POContactsRestore.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");
                    FileDescriptor fdd = fd.getFileDescriptor();
                    InputStream in = new FileInputStream(fdd);
                    //FileInputStream fis = fd.createInputStream();
                    Log.d("BLA BLA", "Len is :: " + (int) fd.getDeclaredLength());
                    byte[] buf = new byte[(int) fd.getDeclaredLength()];
                    in.read(buf);
                    fd.close();
                    String VCard = new String(buf);
                    String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
                    FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
                    mFileOutputStream.write(VCard.toString().getBytes());
                    mFileOutputStream.close();
                    in.close();
                    phones.moveToNext();
                    Log.d("Vcard", VCard);

                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    }
于 2013-02-14T09:22:33.243 回答