0

The following code is used to backup contacts as vcf file. This works fine on devices below api 23 but in higher api's i get java.lang.NegativeArraySizeException: -1. any idea why? Thanks

here is the complete code and error log

 private void getVcardString() throws IOException {
            // TODO Auto-generated method stub
            //ProgressBar pro = (ProgressBar)findViewById(R.id.pb);
            try {


            // ProgressBar pro = (ProgressBar) findViewById(R.id.pb1);
            vCard = new ArrayList<String>();  // Its global....
            cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1",
                    null, null, null);
            if (cursor != null && cursor.getCount() > 0) {
                int i;
                String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
                FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
                cursor.moveToFirst();
                for (i = 0; i < cursor.getCount(); i++) {

                    get(cursor);
                    Log.d("TAG", "Contact " + (i + 1) + "VcF String is" + vCard.get(i));
                    cursor.moveToNext();

                    mFileOutputStream.write(vCard.get(i).toString().getBytes());
                }
                mFileOutputStream.close();
                cursor.close();
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        tx.setText("");
                        pro.setVisibility(View.GONE);
                        btn3.setVisibility(View.VISIBLE);
                        tx1.setVisibility(View.VISIBLE);
                        Toast toast = Toast.makeText(getApplicationContext(), R.string.backupdone, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 90);
                        toast.show();

                    }
                });




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

        private void get(Cursor cursor2) {
            String lookupKey = cursor.getString(cursor.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);
                String vcardstring = new String(buf);
                vCard.add(vcardstring);

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    });

Error Log

07-03 00:20:55.796 9500-10477/com.HEbackup W/System.err: 
java.lang.NegativeArraySizeException: -1
07-03 00:20:55.796 9500-10477/com.HEbackup W/System.err:     at 
app.HEbackup.cbackup$1.get(cbackup.java:212)
07-03 00:20:55.796 9500-10477/com.HEbackup W/System.err:     at 
app.HEbackup.cbackup$1.getVcardString(cbackup.java:172)
07-03 00:20:55.796 9500-10477/com.HEbackup W/System.err:     at 
app.HEbackup.cbackup$1.access$000(cbackup.java:120)
07-03 00:20:55.797 9500-10477/com.HEbackup W/System.err:     at 
app.HEbackup.cbackup$1$1.run(cbackup.java:144)
07-03 00:20:55.797 9500-10477/com.HEbackup W/System.err:     at 
java.lang.Thread.run(Thread.java:761)

2 Answers 2

2

fd.getDeclaredLength() returns -1, which you try to use to create an array. Since an array with a negative length cannot exist you get a NegativeArraySizeException.

Since it seems like the specified asset does not have a predefined length using a BufferedReader (and an InputStreamReader) to handle reading the contents and the conversion to a String would be the correct solution.

Sign up to request clarification or add additional context in comments.

1 Comment

forgive me but what should i convert to string?
0
FileInputStream fis = new FileInputStream(fd.getFileDescriptor());
    byte[] buf = new byte[fis.available()];
    fis.read(buf);
    String vcardstring = new String(buf);
    vCard.add(vcardstring);

1 Comment

Please edit your answer to describe what exactly is different from the asker's version. Code-only answers are poor practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.