0

I'm trying to read a dir and within this dir I have files for each contact. I want to to be able to read those contacts and then put them into the listview. I have tried but, I get a Java error and tried to research it more and found out when I try to do:

fileInputStream fi = fileInput(sdcard/contacts, 0);
fi.read();
//then the parse.

As I said I tried this and get an error which means I can't access a dir with the method above as that method is looking for app specific data.

My Code so far:

private void checkFosImprtCtacs() {
        String FILENAME = "check";
        String RND = "LewisR";
        OutputStream out = null;
        // TODO Auto-generated method stub
        /*
         * Check if the dir exists, if it does we'll load the contacts, if it
         * doesn't the user will be presented with a dialog box and if they hit
         * yes the contacts will be written to the SD card see the
         * importcontacts theory file.
         */
        File dir = new File(Environment.getExternalStorageDirectory()
                + "/chckcontacts");
        if (dir.exists() && dir.isDirectory()) {
            dir.exists();
            dir.getPath().getBytes().toString(
                    //yes I KNOW THIS CODE IS WRONG, I JUST WANTED TO SHOW SOMETHING.
            BufferedReader inputReader = null;
            while (dir.getPath().getBytes() != null) {

            }




        } else {
            if (dir.exists())
                ;
            {
                dir.mkdir();
                // import contacts via dialog box.
                new AlertDialog.Builder(this)
                        .setTitle("Import Contacts")
                        .setMessage("Do you want to import your contacts?")
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        Cursor cursor = getContacts();
                                        while (cursor.moveToNext()) {
                                            String displayName = cursor.getString(cursor
                                                    .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                                            Log.e(displayName, displayName);
                                            // create a File object for the
                                            // parent directory
                                            File Contacts = new File(
                                                    Environment
                                                            .getExternalStorageDirectory()
                                                            + "/contacts");
                                            // have the object build the
                                            // directory structure, if needed.
                                            Contacts.mkdirs();
                                            // create a File object for the
                                            // output file
                                            File outputFile = new File(
                                                    Contacts,
                                                    displayName);
                                            // now attach the OutputStream to
                                            // the file object, instead of a
                                            // String representation
                                            try {
                                                FileOutputStream fos = new FileOutputStream(
                                                        outputFile);
                                            } catch (FileNotFoundException e) {
                                                // TODO Auto-generated catch
                                                // block
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                })
                        .setNegativeButton("No",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        // do nothing
                                    }
                                }).show();

            }
        }
    }

    private Cursor getContacts() {
        // TODO Auto-generated method stub
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = 1";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";
        return managedQuery(uri, projection, selection, selectionArgs,
                sortOrder);

    }
TheBlueCat
  • 1,147
  • 5
  • 19
  • 38
  • Did you add the permission to access the SDCard? What does the logcat say? – CaseyB May 17 '12 at 18:17
  • Just to note, I don't want crash advice, all I want is the functions. Adding the logcat wouldn't help as it's old cat and it clutters up the thread. – TheBlueCat May 17 '12 at 18:20
  • Ok, the functions you use to read a file in Java would be a FileInputStream on which you would call read(); – CaseyB May 17 '12 at 18:22
  • I know. Yet, I get the error I mentioned. – TheBlueCat May 17 '12 at 18:24
  • FileInputStream is for private data for you App. – TheBlueCat May 17 '12 at 18:24
  • 1
    FileInputStream is how you read any file in Java. I know that is what you have, that is why I tried to recommend a debug tactic. You said all you wanted were the functions, so there you go. FileInputStream.read(); – CaseyB May 17 '12 at 19:13
  • Thanks, I tried that and nothing displayed I think I messed up my locations. – TheBlueCat May 17 '12 at 21:02

0 Answers0