将联系人导出到vCard时出现错误“ java.lang.NegativeArraySizeException:-1”

时间:2019-04-15 10:49:32

标签: android contacts vcard

我想将联系人导出到vCard文件,同时在stackoverflow中搜索时遇到以下代码,

import android.app.Activity;
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;

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

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

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vcard);
    vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf";
    /**This Function For Vcard And here i take one Array List in Which i store every Vcard String of Every Conatact
     * Here i take one Cursor and this cursor is not null and its count>0 than i repeat one loop up to cursor.getcount() means Up to number of phone contacts.
     * And in Every Loop i can make vcard string and store in Array list which i declared as a Global.
     * And in Every Loop i move cursor next and print log in logcat.
     * */
    getVcardString();

}
private void getVcardString() {
    // TODO Auto-generated method stub
    vCard = new ArrayList<String>();
    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    if(cursor!=null&&cursor.getCount()>0)
    {
        cursor.moveToFirst();
        for(int i =0;i<cursor.getCount();i++)
        {
            Log.d("TAG", "count -> "+cursor.getCount());
            get(cursor);
            //Log.d("TAG", "Contact "+(i+1)+"VcF String is"+vCard.get(i));
            cursor.moveToNext();
        }

    }
    else
    {
        Log.d("TAG", "No Contacts in Your Phone");
    }

}

public void get(Cursor cursor)
{
    //cursor.moveToFirst();
    String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
    AssetFileDescriptor fd;
    try {
        fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");

        // Your Complex Code and you used function without loop so how can you get all Contacts Vcard.??


       /* 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 out = new FileOutputStream(path);
        out.write(VCard.toString().getBytes());
        Log.d("Vcard",  VCard);*/

        FileInputStream fis = fd.createInputStream();
        byte[] buf = new byte[(int) fd.getDeclaredLength()];
        fis.read(buf);
        String vcardstring= new String(buf);
        vCard.add(vcardstring);

        String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
        Log.i("TAG", "Path : "+storage_path);
        FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
        mFileOutputStream.write(vcardstring.toString().getBytes());

    } catch (Exception e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        Log.e("TAG", "error -> "+e1);
    }
}
}

但是我遇到了异常,如“ java.lang.NegativeArraySizeException:-1”,并且在stackoverflow中没有针对此问题的解决方案。

2019-04-15 16:04:39.639 23340-23340/com.ninositsolutions.backuptest E/TAG: error -> java.lang.NegativeArraySizeException: -1

不知道为什么会发生此错误,即使我检查了游标的计数并显示为零以上。

0 个答案:

没有答案
相关问题