递归搜索外部存储中的txt文件并将它们放在列表视图中。当线程运行时,应该有一个processdialog。它在我的手机(Android 5.1和Android 4.4)上运行良好,但是当我改为6.0时,它工作了一次,之后对话框无法显示。我认为存储路径存在错误(使用Environment.getExternalStorageDirectory()。toString(); )。 如果我使用身体记忆作为默认值,我会得到路径" / storage / emulated / 0"如果我使用外部存储作为默认值,我得到路径" /storage.164F-F6FD"。线程也可以工作,但没有文件加载。
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();
}
}
寻求帮助
答案 0 :(得分:0)
&#34;从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。此方法简化了应用安装过程,因为用户在安装或更新应用时无需授予权限。&#34;
将其更改为
private void requestPermission(String permission){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (ContextCompat.checkSelfPermission(BookListActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(BookListActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
}
}
感谢第一条评论 https://developer.android.com/training/permissions/requesting.html