android 文件浏览。打开sdcard文件夹。只显示文件夹和txt文件。选择txt文件返回其名字

来源:互联网 发布:金税数据导出软件 编辑:程序博客网 时间:2024/06/05 10:22

提供源代码下载地址:http://download.csdn.net/detail/kankankankan2222/4484622

打开sdcard文件夹。只显示文件夹和txt文件。过滤非txt文件。选择txt文件返回其名字。

主类:SdcardfileActivity.java

 

public class SdcardfileActivity  extends Activity {

   /** Called when the activity is firstcreated. */

    public static final int FILE_RESULT_CODE= 1;

    private TextViewtextView;

   

   @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(new OnClickListener() {

            public void onClick(View v) {

                Intentintent = newIntent(SdcardfileActivity.this,MyFileManager.class);

                startActivityForResult(intent,FILE_RESULT_CODE);

            }

        });

    }

   

    @Override

    protected void onActivityResult(int requestCode,int resultCode, Intentdata) {

        if(FILE_RESULT_CODE== requestCode){

            Bundlebundle = null;

            if(data!=null&&(bundle=data.getExtras())!=null){

                textView.setText("选择文件夹为:"+bundle.getString("file"));

            }

        }

    }

}

//////////////////

第二个类:MyFileManager.java

 

public classMyFileManager extends ListActivity {

    private List<String>items =null;

    private List<String>paths =null;

    private StringrootPath = getSDDir();

    private StringcurPath = getSDDir();

    private TextViewmPath;

 

    @Override

    protected void onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.fileselect);

        mPath = (TextView)findViewById(R.id.mPath);

        getFileDir(rootPath);

    }

 

    private void getFileDir(StringfilePath) {

        mPath.setText(filePath);

        items = newArrayList<String>();

        paths = newArrayList<String>();

        File f = new File(filePath);

        File[]files = f.listFiles();

        if (!filePath.equals(rootPath)) {

            items.add("b1");

            paths.add(rootPath);

            items.add("b2");

            paths.add(f.getParent());

        }

        for (int i = 0; i < files.length; i++) {

            Filefile = files[i];

            // check file is txt file or not

            // if is,add into list to show

            if (checkShapeFile(file)){

                items.add(file.getName());

                paths.add(file.getPath());

            }

        }

 

        setListAdapter(new MyAdapter(this,items,paths));

    }

 

    // open allocate file

    @Override

    protected voidonListItemClick(ListView l, View v, int position,long id) {

        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 boolean checkShapeFile(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 final String getSDDir() {

        if (!checkSDcard()) {

            Toast.makeText(this,"no sdcard",Toast.LENGTH_SHORT).show();

            return"";

        }

        try {

            StringSD_DIR = Environment.getExternalStorageDirectory()

                    .toString();

            return SD_DIR;

        }catch(Exception e) {

            return"";

        }

    }

 

    public boolean checkSDcard() {

        StringsdStutusString = Environment.getExternalStorageState();

        if(sdStutusString.equals(Environment.MEDIA_MOUNTED)) {

            return true;

        }else{

            return false;

        }

    }

 

}

 

//////////////////////

自定义的适配器类:MyAdapter.java

public class MyAdapter extends BaseAdapter {

      private LayoutInflatermInflater;

      private BitmapmIcon1;

      private BitmapmIcon2;

      private BitmapmIcon3;

      private BitmapmIcon4;

      private List<String>items;

      private List<String>paths;

      public MyAdapter(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();

      }

 

      public Object getItem(int position)

      {

        returnitems.get(position);

      }

     

      public longgetItemId(intposition)

      {

        return position;

      }

     

      public View getView(int position,View convertView,ViewGroup parent)

      {

        ViewHolder holder;

       

        if(convertView ==null)

        {

          convertView = mInflater.inflate(R.layout.file_row,null);

          holder = new ViewHolder();

          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=new File(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);

          }

        }

        return convertView;

      }

      private classViewHolder

      {

        TextView text;

        ImageView icon;

      }

}



//////////////////////////

下面是xml文件:

Main.xml:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/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="http://schemas.android.com/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="http://schemas.android.com/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>




原创粉丝点击