《android遍历SD卡所有文件源程序两例-注释.docx》由会员分享,可在线阅读,更多相关《android遍历SD卡所有文件源程序两例-注释.docx(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、程序一:遍历SD卡中所有文件并在listview中显示package my.sd;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.widget.Button;import android.widget.ListView;import android.widget.Simpl
2、eAdapter;import android.widget.TextView;public class testSD extends Activity /view TextView tView ;ListView i ;Button btn ; /data private ArrayList list ; /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); set
3、ContentView(R.layout.main); tView = (TextView)findViewById(R.id.textview1); /btn = (Button) findViewById(R.id.go); tView = new TextView(this); tView.setText(get all files in your sd card); /init data i = (ListView) findViewById(R.id.music_list); list = new ArrayList(); getAllFiles(new File(/sdcard);
4、 SimpleAdapter ladapter = new SimpleAdapter(this,getMapData(list),R.layout.relative, new StringItemText,ItemTitle,new intR.id.ItemTitle,R.id.ItemText); i.setAdapter(ladapter); TextView empty = new TextView(this); empty.setText(empty!); i.setEmptyView(empty); private ArrayListMap getMapData(ArrayList
5、 list) ArrayListMap data = new ArrayListMap(); HashMap item; int i = 0 ; for(i=0;ilist.size();i+) item = new HashMap(); String path = list.get(i).toString(); String name = path.substring(path.lastIndexOf(/)+1,path.length(); /保存每一格list单元格的数据 , item.put(ItemText,name); item.put(ItemTitle, path); data.
6、add(item); return data; private void getAllFiles(File root)File files = root.listFiles();if(files != null)for(File f:files)if(f.isDirectory()getAllFiles(f);elsethis.list.add(f); 程序二:更简单的SD卡文件遍历程序package com.wenhao.test.sddemo;import java.io.File;import android.app.Activity;import android.os.Bundle;i
7、mport android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainDemo extends Activity /* Called when the activity is first created. */ private Button button = null; private File path; Override pu
8、blic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button)findViewById(R.id.mybutton); /检测SD卡是否存在 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED) path = Environment.getExternalStorageDirectory(); els
9、e Toast.makeText(this, 没有SD卡, Toast.LENGTH_LONG).show(); finish(); button.setOnClickListener(new OnClickListener() Override public void onClick(View v) / TODO Auto-generated method stub getAllFiles(path); ); / 遍历接收一个文件路径,然后把文件子目录中的所有文件遍历并输出来 private void getAllFiles(File root) File files = root.listFiles(); if(files != null) for (File f : files) if(f.isDirectory() getAllFiles(f); else System.out.println(f);