热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android获取sd卡目录失败_获取sd卡存储路径失败

android新手,最近在学习搜索sd卡上的所有txt文件并将其放入listview中显示,加载的过程中界面上会显示一个progressdialog。在

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 file_name;

private static List file_txt_path;

private MyBookAdapter adapter;

private File file;

private List> 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 mPath = data.getStringArrayListExtra("file");

for (int i = 0; i

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卡,均不能加载



推荐阅读
author-avatar
我的生活我做主哦耶_266
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有