Recursive searching txt files in external storage and put them in a listview. When the thread is running ,there should be a processdialog. It worked well on my phones(android 5.1 and android 4.4), but when I change to 6.0, it worked once and after that the dialog can not show. I persume that there is sth wrong with the storage path(using Environment.getExternalStorageDirectory().toString();). If I use the body memory as default, I get the path "/storage/emulated/0" and if I use the external storage as default, I get the path "/storage.164F-F6FD".The thread will also work but no files are load.
public class BookListActivity extends Activity {
private static List<String> file_name;
private static List<String> file_txt_path;
private MyBookAdapter adapter;
private File file;
private List<Map<String, String>> listItems;
private MultiModeCallback mCallback;
private String mExternalStoragePath;
private Handler mHandler;
private ListView mListView;
private ProgressDialog mProgressDialog;
/**
* 接收返回的路径
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("com.ldgforever.jianreader", "receivingPath");
if (data != null) {
Log.d("com.ldgforever.jianreader", "onActivityResult");
ArrayList<String> mPath = data.getStringArrayListExtra("file");
for (int i = 0; i < mPath.size(); i++) {
File pathFile = new File(mPath.get(i));
Map<String, String> pathMap = new HashMap<String, String>();
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<String>();
file_txt_path = new ArrayList<String>();
listItems = new ArrayList<Map<String, String>>();
listItems = savedataListMap.getInfo(BookListActivity.this, "ListMap");
if (listItems.isEmpty()) {
BookAddingDialog();
} else {
ShowTxtFilesInList(listItems);
}
mHandler = new Handler() {
public void handleMessage(Message message) {
switch (message.what) {
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<Map<String, String>>();
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<Map<String,String>>中的书籍信息显示到ListView中
*
* @param listItems
*/
private void ShowTxtFilesInList(final List<Map<String, String>> listItems) {
if (file_name != null) {
for (int i = 0; i < file_name.size(); i++) {
HashMap<String, String> hashmap = new HashMap<String, String>();
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();
}
}
ask for help