android新手,最近在学习 搜索sd卡上的所有txt文件并将其放入listview中显示,加载的过程中界面上会显示一个progress dialog。在之前的手机(Android5.0 android4.4)上都能正常的搜索到,但最近新换了一个手机(6.0) 发现logcat能够正常打印出路径 handle也能接收到搜索完成返回的message,但是没有扫描并放入listview 也没有显示progressdialog。求助
代码如下
package com.ldgforever.jianreader;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ldgforever.savedata.savedataListMap;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
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()) {
listItems = new ArrayList<Map<String, String>>();
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<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();
}
}
logcat无报错
我试了默认存储位置为机身和SD卡,均不能加载
Cuba gunakan
Environment.getExternalStorageDirectory().getPath()
untuk mendapatkan laluan ke direktori akar kad SD.