File pathFile = new File(mPath.get(i));
Map pathMap = new HashMap();
if (pathFile.exists()) {
if (pathFile.getName().endsWith(".txt")) {
pathMap.put("Name", pathFile.getName());
pathMap.put("Path", pathFile.getPath());
listItems.add(pathMap);
savedataListMap.saveInfo(BookListActivity.this, "ListMap", listItems);
ShowTxtFilesInList(listItems);
} else {
Toast.makeText(BookListActivity.this, "请选择一个txt文件!", 0).show();
;
}
}
}
}
}
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_book_list);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setCancelable(false);
mProgressDialog.setMessage("正在搜索书籍,请稍候 ……");
mExternalStoragePath = Environment.getExternalStorageDirectory().toString();
Log.i("JianReader", mExternalStoragePath);
file = new File(mExternalStoragePath);
file_name = new ArrayList();
file_txt_path = new ArrayList();
listItems = new ArrayList>();
listItems = savedataListMap.getInfo(BookListActivity.this, "ListMap");
if (listItems.isEmpty()) {
listItems = new ArrayList>();
mProgressDialog.show();
new Thread() {
public void run() {
Log.i("JianReader", "listItem is Empty");
listFileTxt(file);
mHandler.sendEmptyMessage(11);
}
}.start();
} else {
ShowTxtFilesInList(listItems);
}
mHandler = new Handler() {
public void handleMessage(Message message) {
switch (message.what) {
case 11:
Log.i("JianReader", message.what + "");
ShowTxtFilesInList(listItems);
savedataListMap.saveInfo(BookListActivity.this, "ListMap", listItems);
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
return;
}
break;
case 12:
Log.i("JianReader", message.what + "");
ShowTxtFilesInList(listItems);
savedataListMap.saveInfo(BookListActivity.this, "ListMap", listItems);
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
return;
}
break;
default:
break;
}
return;
}
};
}
/**
* 书籍添加对话框
*/
private void BookAddingDialog() {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setTitle("请选择添加书籍的方式");
builder.setPositiveButton("扫描SDCard添加", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
listItems = new ArrayList>();
mProgressDialog.show();
new Thread() {
public void run() {
listFileTxt(file);
mHandler.sendEmptyMessage(12);
}
}.start();
}
});
builder.setNegativeButton("选择路径添加", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
Intent intent = new Intent(BookListActivity.this, MyFileManager.class);
startActivityForResult(intent, 2);
}
});
builder.setNeutralButton("稍后手动添加", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
dialoginterface.dismiss();
}
});
builder.create().show();
}
/**
* 将保存在List>中的书籍信息显示到ListView中
*
* @param listItems
*/
private void ShowTxtFilesInList(final List> listItems) {
if (file_name != null) {
for (int i = 0; i HashMap hashmap = new HashMap();
hashmap.put("Name", (String) file_name.get(i));
hashmap.put("Path", (String) file_txt_path.get(i));
listItems.add(hashmap);
}
adapter = new MyBookAdapter(this, listItems);
mCallback = new MultiModeCallback();
mListView = (ListView) findViewById(R.id.booklist);
mListView.setChoiceMode(3); // Multi
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Intent ii = new Intent(BookListActivity.this, ReadingActivity.class);
String itemPath = listItems.get(position).get("Path");
ii.putExtra("mItemPath", itemPath);
startActivity(ii);
}
});
mListView.setMultiChoiceModeListener(mCallback);
mListView.setAdapter(adapter);
} else {
failAddingDialog();
return;
}
}
/**
* 添加书籍失败对话框
*/
private void failAddingDialog() {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setTitle("添加书籍失败");
builder.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
dialoginterface.dismiss();
}
});
}
/**
* 递归查找SD卡上所有书籍
*
* @param file
*/
public static void listFileTxt(File file) {
File[] files = file.listFiles();
try {
for (File f : files) {
if (!f.isDirectory()) {
if (f.getName().endsWith(".txt")) {
long size = f.length();
if (size > 50 * 1024) {
file_name.add(f.getName());
file_txt_path.add(f.getAbsolutePath());
}
}
} else if (f.isDirectory()) {
listFileTxt(f);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
logcat无报错
我试了默认存储位置为机身和SD卡,均不能加载