从SD卡上的目录中读取文件

时间:2012-05-17 18:07:20

标签: java android file dir

我正在尝试阅读目录,在这个目录中我有每个联系人的文件。我希望能够阅读这些联系人,然后将它们放入列表视图中。我试过但是,我得到了一个Java错误,并试图更多地研究它,并在我尝试时发现:

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

正如我所说,我尝试了这个并得到一个错误,这意味着我无法通过上述方法访问目录,因为该方法正在寻找特定于应用的数据。

我的代码到目前为止:

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);

    }

0 个答案:

没有答案