提供源代码下载地址:/detail/kankankankan2222/4484622
打开sdcard文件夹。只显示文件夹和txt文件。过滤非txt文件。选择txt文件返回其名字。
主类:SdcardfileActivity.java
public class SdcardfileActivityextendsActivity {
/** Called when the activity is firstcreated. */
public static final intFILE_RESULT_CODE= 1;
privateTextViewtextView;
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button =(Button)findViewById(R.id.button);
textView =(TextView)findViewById(R.id.fileText);
button.setOnClickListener(newOnClickListener() {
public voidonClick(View v) {
Intentintent =newIntent(SdcardfileActivity.this,MyFileManager.class);
startActivityForResult(intent,FILE_RESULT_CODE);
}
});
}
@Override
protected voidonActivityResult(intrequestCode,intresultCode, Intentdata) {
if(FILE_RESULT_CODE== requestCode){
Bundlebundle =null;
if(data!=null&&(bundle=data.getExtras())!=null){
textView.setText("选择文件夹为:"+bundle.getString("file"));
}
}
}
}
//
第二个类:MyFileManager.java
public classMyFileManagerextendsListActivity {
privateList<String>items =null;
privateList<String>paths =null;
privateStringrootPath = getSDDir();
privateStringcurPath= getSDDir();
privateTextViewmPath;
@Override
protected voidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fileselect);
mPath = (TextView)findViewById(R.id.mPath);
getFileDir(rootPath);
}
private voidgetFileDir(StringfilePath) {
mPath.setText(filePath);
items =newArrayList<String>();
paths =newArrayList<String>();
File f =newFile(filePath);
File[]files = f.listFiles();
if(!filePath.equals(rootPath)) {
items.add("b1");
paths.add(rootPath);
items.add("b2");
paths.add(f.getParent());
}
for(inti = 0; i < files.length; i++) {
Filefile = files[i];
// check file istxtfile or not
// if is,add into list to show
if(checkShapeFile(file)){
items.add(file.getName());
paths.add(file.getPath());
}
}
setListAdapter(newMyAdapter(this,items,paths));
}
// open allocate file
@Override
protected voidonListItemClick(ListView l, View v,intposition,longid) {
Filefile =newFile(paths.get(position));
if(file.isDirectory()) {
curPath = paths.get(position);
getFileDir(paths.get(position));
}else{
Intentdata =newIntent(MyFileManager.this,
SdcardfileActivity.class);
Bundlebundle =newBundle();
bundle.putString("file",file.getPath());
data.putExtras(bundle);
setResult(2,data);
finish();
}
}
public booleancheckShapeFile(Filefile) {
StringfileNameString = file.getName();
StringendNameString = fileNameString.substring(
fileNameString.lastIndexOf(".") + 1,fileNameString.length())
.toLowerCase();
// file is directory or not
if(file.isDirectory()) {
return true;
}
if(endNameString.equals("txt")) {
return true;
}else{
return false;
}
}
protected finalString getSDDir() {
if(!checkSDcard()) {
Toast.makeText(this,"no sdcard",Toast.LENGTH_SHORT).show();
return"";
}
try{
StringSD_DIR = Environment.getExternalStorageDirectory()
.toString();
returnSD_DIR;
}catch(Exception e) {
return"";
}
}
public booleancheckSDcard() {
StringsdStutusString = Environment.getExternalStorageState();
if(sdStutusString.equals(Environment.MEDIA_MOUNTED)) {
return true;
}else{
return false;
}
}
}
//
自定义的适配器类:MyAdapter.java
public class MyAdapter extends BaseAdapter {
privateLayoutInflatermInflater;
privateBitmapmIcon1;
privateBitmapmIcon2;
privateBitmapmIcon3;
privateBitmapmIcon4;
privateList<String>items;
privateList<String>paths;
publicMyAdapter(Context context,List<String> it,List<String> pa)
{
mInflater = LayoutInflater.from(context);
items = it;
paths = pa;
mIcon1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.back01);
mIcon2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.back02);
mIcon3 = BitmapFactory.decodeResource(context.getResources(),R.drawable.folder);
mIcon4 = BitmapFactory.decodeResource(context.getResources(),R.drawable.doc);
}
public intgetCount()
{
returnitems.size();
}
publicObject getItem(intposition)
{
returnitems.get(position);
}
public longgetItemId(intposition)
{
returnposition;
}
publicView getView(intposition,View convertView,ViewGroup parent)
{
ViewHolder holder;
if(convertView ==null)
{
convertView = mInflater.inflate(R.layout.file_row,null);
holder =newViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
File f=newFile(paths.get(position).toString());
if(items.get(position).toString().equals("b1"))
{
holder.text.setText("返回根目录..");
holder.icon.setImageBitmap(mIcon1);
}
else if(items.get(position).toString().equals("b2"))
{
holder.text.setText("返回上一层..");
holder.icon.setImageBitmap(mIcon2);
}
else
{
holder.text.setText(f.getName());
if(f.isDirectory())
{
holder.icon.setImageBitmap(mIcon3);
}
else
{
holder.icon.setImageBitmap(mIcon4);
}
}
returnconvertView;
}
private classViewHolder
{
TextView text;
ImageView icon;
}
}
//
下面是xml文件:
Main.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选择文件"
/>
<TextView
android:id="@+id/fileText"
android:gravity="center"
android:textSize="20px"
android:textColor="#219ac6"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
/
显示文件列表的xml:
Fileselect.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- -->
<TextView
android:id="@+id/mPath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="18sp">
</TextView>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
//
List的单个item的布局:
file_row.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="6px">
<ImageView
android:id="@+id/icon"
android:layout_width="30dip"
android:layout_height="30dip">
</ImageView>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
</TextView>
</LinearLayout>
</LinearLayout>
/
在androidManifest.xml中:
加:
<activity
android:name=".MyFileManager"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
></activity>