先看看效果图: package wuwang.tools.utils; import java.io.File; import java.io.FileInputS
先看看效果图:
package wuwang.tools.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFORMat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import Android.os.Environment;
public class GetFilesUtils {
public static final String FILE_TYPE_FOLDER="wFl2d";
public static final String FILE_INFO_NAME="fName";
public static final String FILE_INFO_ISFOLDER="fIsDir";
public static final String FILE_INFO_TYPE="fFileType";
public static final String FILE_INFO_NUM_SONDIRS="fSonDirs";
public static final String FILE_INFO_NUM_SONFILES="fSonFiles";
public static final String FILE_INFO_PATH="fPath";
private static GetFilesUtils gfu;
private GetFilesUtils(){
}
public static synchronized GetFilesUtils getInstance(){
if(gfu==null){
gfu=new GetFilesUtils();
}
return gfu;
}
public List<Map<String, Object>> getSonnode(File path){
if(path.isDirectory()){
List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
File[] files=path.listFiles();
if(files!=null){
for(int i=0;i<files.length;i++){
Map<String, Object> fileInfo=new HashMap<String, Object>();
fileInfo.put(FILE_INFO_NAME, files[i].getName());
if(files[i].isDirectory()){
fileInfo.put(FILE_INFO_ISFOLDER, true);
File[] bFiles=files[i].listFiles();
if(bFiles==null){
fileInfo.put(FILE_INFO_NUM_SONDIRS, 0);
fileInfo.put(FILE_INFO_NUM_SONFILES, 0);
}else{
int getNumOfDir=0;
for(int j=0;j<bFiles.length;j++){
if(bFiles[j].isDirectory()){
getNumOfDir++;
}
}
fileInfo.put(FILE_INFO_NUM_SONDIRS, getNumOfDir);
fileInfo.put(FILE_INFO_NUM_SONFILES, bFiles.length-getNumOfDir);
}
fileInfo.put(FILE_INFO_TYPE, FILE_TYPE_FOLDER);
}else{
fileInfo.put(FILE_INFO_ISFOLDER, false);
fileInfo.put(FILE_INFO_NUM_SONDIRS, 0);
fileInfo.put(FILE_INFO_NUM_SONFILES, 0);
fileInfo.put(FILE_INFO_TYPE, getFileType(files[i].getName()));
}
fileInfo.put(FILE_INFO_PATH, files[i].getAbsoluteFile());
list.add(fileInfo);
}
return list;
}else{
return null;
}
}else{
return null;
}
}
public List<Map<String, Object>> getSonNode(String pathStr){
File path=new File(pathStr);
return getSonNode(path);
}
public List<Map<String, Object>> getBrotherNode(File path){
if(path.getParentFile()!=null){
return getSonNode(path.getParentFile());
}else{
return null;
}
}
public List<Map<String, Object>> getBrotherNode(String pathStr){
File path=new File(pathStr);
return getBrotherNode(path);
}
public String getParentPath(File path){
if(path.getParentFile()==null){
return null;
}else{
return path.getParent();
}
}
public String getParentPath(String pathStr){
File path=new File(pathStr);
if(path.getParentFile()==null){
return null;
}else{
return path.getParent();
}
}
public String getSDPath(){
String sdcard=Environment.getExternalStorageState();
if(sdcard.equals(Environment.MEDIA_MOUNTED)){
return Environment.getExternalStorageDirectory().getAbsolutePath();
}else{
return null;
}
}
public String getBasePath(){
String basePath=getSDPath();
if(basePath==null){
return Environment.getDataDirectory().getAbsolutePath();
}else{
return basePath;
}
}
public String getFileSize(File path) throws IOException{
if(path.exists()){
DecimalFormat df = new DecimalFormat("#.00");
String sizeStr="";
FileInputStream fis=new FileInputStream(path);
long size=fis.available();
fis.close();
if(size<1024){
sizeStr=size+"B";
}else if(size<1048576){
sizeStr=df.format(size/(double)1024)+"KB";
}else if(size<1073741824){
sizeStr=df.format(size/(double)1048576)+"MB";
}else{
sizeStr=df.format(size/(double)1073741824)+"GB";
}
return sizeStr;
}else{
return null;
}
}
public String getFileSize(String fpath){
File path=new File(fpath);
if(path.exists()){
DecimalFormat df = new DecimalFormat("#.00");
String sizeStr="";
long size=0;
try {
FileInputStream fis = new FileInputStream(path);
size=fis.available();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "未知大小";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "未知大小";
}
if(size<1024){
sizeStr=size+"B";
}else if(size<1048576){
sizeStr=df.format(size/(double)1024)+"KB";
}else if(size<1073741824){
sizeStr=df.format(size/(double)1048576)+"MB";
}else{
sizeStr=df.format(size/(double)1073741824)+"GB";
}
return sizeStr;
}else{
return "未知大小";
}
}
public String getFileType(String fileName){
if(fileName!=""&&fileName.length()>3){
int dot=fileName.lastIndexOf(".");
if(dot>0){
return fileName.substring(dot+1);
}else{
return "";
}
}
return "";
}
public Comparator<Map<String, Object>> defaultOrder() {
final String orderBy0=FILE_INFO_ISFOLDER;
final String orderBy1=FILE_INFO_TYPE;
final String orderBy2=FILE_INFO_NAME;
Comparator<Map<String, Object>> order=new Comparator<Map<String,Object>>() {
@Override
public int compare(Map<String, Object> lhs, Map<String, Object> rhs) {
// TODO Auto-generated method stub
int left0=lhs.get(orderBy0).equals(true)?0:1;
int right0=rhs.get(orderBy0).equals(true)?0:1;
if(left0==right0){
String left1=lhs.get(orderBy1).toString();
String right1=rhs.get(orderBy1).toString();
if(left1.compareTo(right1)==0){
String left2=lhs.get(orderBy2).toString();
String right2=rhs.get(orderBy2).toString();
return left2.compareTo(right2);
}else{
return left1.compareTo(right1);
}
}else{
return left0-right0;
}
}
};
return order;
}
}
使用方法:List<Map<String, Object>> list=GetFilesUtils.getInstance().getSonNode(file); //或其他方法
使用示例:
package wuwang.mypage.activity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import wuwang.ebookworm.R;
import wuwang.tools.utils.GetFilesUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class FolderActivity extends Activity implements OnItemClickListener,OnClickListener {
private ListView folderLv;
private TextView foldernowTv;
private SimpleAdapter sAdapter;
private List<Map<String, Object>> aList;
private String baseFile;
private TextView titleTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mypage_folder);
baseFile=GetFilesUtils.getInstance().getBasePath();
titleTv=(TextView) findViewById(R.id.mtitle);
titleTv.setText("本地文件");
folderLv=(ListView) findViewById(R.id.folder_list);
foldernowTv=(TextView) findViewById(R.id.folder_now);
foldernowTv.setText(baseFile);
foldernowTv.setOnClickListener(this);
aList=new ArrayList<Map<String,Object>>();
sAdapter=new SimpleAdapter(this, aList,R.layout.listitem_folder, new String[]{"fImg","fName","fInfo"},
new int[]{R.id.folder_img,R.id.folder_name,R.id.folder_info});
folderLv.setAdapter(sAdapter);
folderLv.setOnItemClickListener(this);
try {
loadFolderList(baseFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void loadFolderList(String file) throws IOException{
List<Map<String, Object>> list=GetFilesUtils.getInstance().getSonNode(file);
if(list!=null){
Collections.sort(list, GetFilesUtils.getInstance().defaultOrder());
aList.clear();
for(Map<String, Object> map:list){
String fileType=(String) map.get(GetFilesUtils.FILE_INFO_TYPE);
Map<String,Object> gMap=new HashMap<String, Object>();
if(map.get(GetFilesUtils.FILE_INFO_ISFOLDER).equals(true)){
gMap.put("fIsDir", true);
gMap.put("fImg",R.drawable.filetype_folder );
gMap.put("fInfo", map.get(GetFilesUtils.FILE_INFO_NUM_SONDIRS)+"个文件夹和"+
map.get(GetFilesUtils.FILE_INFO_NUM_SONFILES)+"个文件");
}else{
gMap.put("fIsDir", false);
if(fileType.equals("txt")||fileType.equals("text")){
gMap.put("fImg", R.drawable.filetype_text);
}else{
gMap.put("fImg", R.drawable.filetype_unknow);
}
gMap.put("fInfo","文件大小:"+GetFilesUtils.getInstance().getFileSize(map.get(GetFilesUtils.FILE_INFO_PATH).toString()));
}
gMap.put("fName", map.get(GetFilesUtils.FILE_INFO_NAME));
gMap.put("fPath", map.get(GetFilesUtils.FILE_INFO_PATH));
aList.add(gMap);
}
}else{
aList.clear();
}
sAdapter.notifyDataSetChanged();
foldernowTv.setText(file);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
try {
if(aList.get(position).get("fIsDir").equals(true)){
loadFolderList(aList.get(position).get("fPath").toString());
}else{
Toast.makeText(this, "这是文件,处理程序待添加", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.folder_now){
try {
String folder=GetFilesUtils.getInstance().getParentPath(foldernowTv.getText().toString());
if(folder==null){
Toast.makeText(this, "无父目录,待处理", Toast.LENGTH_SHORT).show();
}else{
loadFolderList(folder);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
页面的布局文件为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/frame_main_bg" >
<include layout="@layout/mypage_title_folder"/>
<TextView android:layout_width="match_parent"
android:layout_height="40dp"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:gravity="center_vertical"
android:ellipsize="start"
android:singleLine="true"
android:drawableLeft="@drawable/folder_backupimg"
android:paddingLeft="10dp"
android:drawablePadding="10dp"
android:id="@+id/folder_now"
android:background="@color/frame_title_bg_clk_color" />
<ListView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/folder_list"
android:dividerHeight="1dp"
android:divider="@color/folder_list_divider" >
</ListView>
</LinearLayout>
您可能感兴趣的文章:Android获取SDcard目录及创建文件夹的方法Android获取本机各种类型文件的方法Android中Java根据文件头获取文件类型的方法Android按时间先后顺序获取目录下文件列表
--结束END--
本文标题: Android获取手机文件夹及文件列表的方法
本文链接: https://lsjlt.com/news/23374.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0