我正在尝试读取一个目录,在这个目录中我有每个联系人的文件。我希望能够阅读这些联系人,然后将它们放入列表视图中。我已经尝试过,但是,我得到了一个 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);
}