[Android实例] 仿Windows 文件浏览 文件选择器

来源:互联网 发布:abc优化算法流程图 编辑:程序博客网 时间:2024/05/16 08:17

最近在做一个音乐播放器的小实例,其中涉及音乐文件选取。通过窗口模式的Activity来选择添加音乐文件或者文件夹。

menuaddgridview.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.   android:orientation="vertical"
  4.   android:layout_width="wrap_content"
  5.   android:layout_height="wrap_content">
  6.     <RelativeLayout
  7.       xmlns:android="http://schemas.android.com/apk/res/android"
  8.       android:orientation="horizontal"
  9.       android:layout_width="match_parent"
  10.       android:layout_height="wrap_content"
  11.       android:background="@drawable/radiogroup_bg1"
  12.       android:padding="5.0dip">
  13.       <ImageButton
  14.           android:id="@+id/MenuAddGridView_button_openfolder"
  15.           android:layout_width="wrap_content" android:layout_height="wrap_content"
  16.           android:layout_marginLeft="10.0dip"
  17.           android:src="@drawable/radiogroup_openfolder"
  18.           android:adjustViewBounds="true"
  19.           android:padding="10.0dip"
  20.           android:layout_alignParentLeft="true"/>
  21.      <ImageButton
  22.           android:id="@+id/MenuAddGridView_button_addSingle"
  23.           android:layout_width="wrap_content" android:layout_height="wrap_content"
  24.           android:layout_marginLeft="10.0dip"
  25.           android:src="@drawable/radiogroup_addsingle"
  26.           android:adjustViewBounds="true"
  27.           android:padding="10.0dip"
  28.           android:layout_toRightOf="@id/MenuAddGridView_button_openfolder"/>
  29.      <ImageButton
  30.           android:id="@+id/MenuAddGridView_button_addFolder"
  31.           android:layout_width="wrap_content" android:layout_height="wrap_content"
  32.           android:layout_marginLeft="10.0dip"
  33.           android:src="@drawable/radiogroup_addfolder"
  34.           android:adjustViewBounds="true"
  35.           android:padding="10.0dip"
  36.           android:layout_toRightOf="@id/MenuAddGridView_button_addSingle"/>
  37.      <ImageButton
  38.           android:id="@+id/MenuAddGridView_button_backFolder"
  39.           android:layout_width="wrap_content" android:layout_height="wrap_content"
  40.           android:layout_marginRight="10.0dip"
  41.           android:src="@drawable/radiogroup_backfolder"
  42.           android:adjustViewBounds="true"
  43.           android:padding="10.0dip"
  44.           android:layout_alignParentRight="true"/>               
  45.     </RelativeLayout>  
  46.     <GridView xmlns:android="http://schemas.android.com/apk/res/android"
  47.         android:id="@+id/MenuAddGridView_gridview"
  48.         android:layout_width="match_parent"
  49.         android:layout_height="match_parent"
  50.         android:numColumns="auto_fit"
  51.         android:verticalSpacing="10dp"
  52.         android:horizontalSpacing="10dp"
  53.         android:columnWidth="90dp"
  54.         android:stretchMode="columnWidth"
  55.         android:gravity="center"
  56.     />

  57. </LinearLayout>
复制代码
menuaddgridview_item.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3.          xmlns:android="http://schemas.android.com/apk/res/android"
  4.          android:layout_height="wrap_content"
  5.          android:paddingBottom="4dip" android:layout_width="fill_parent">
  6.          <ImageView
  7.                android:layout_height="wrap_content"
  8.                android:id="@+id/MenuAddGridView_ItemImage"
  9.                android:layout_width="wrap_content"
  10.                android:layout_centerHorizontal="true">
  11.          </ImageView>
  12.          <TextView
  13.                android:layout_width="wrap_content"
  14.                android:layout_below="@id/MenuAddGridView_ItemImage"
  15.                android:layout_height="wrap_content"
  16.                android:layout_centerHorizontal="true"
  17.                android:id="@+id/MenuAddGridView_ItemText">
  18.          </TextView>
  19. </RelativeLayout>
复制代码

设置Activity为窗口模式


<activity android:name=".MenuAddGridView" android:theme="@android:style/Theme.Dialog"></activity>

记得要添加权限


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

MenuAddGridView的代码:

  1. public class MenuAddGridView extends Activity {
  2.     GridView gridview;
  3.     SimpleAdapter gridviewAdapter;
  4.     ImageButton openFolder,addSingle,addFolder,backFolder;
  5.     String sdcardFilePath,thisFilePath,selectFilePath;
  6.    
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
  10.         this.setContentView(R.layout.menuaddgridview);
  11.         
  12.         sdcardFilePath=Environment.getExternalStorageDirectory().getAbsolutePath();//得到sdcard目录
  13.         thisFilePath=sdcardFilePath;
  14.         
  15.         openFolder=(ImageButton) this.findViewById(R.id.MenuAddGridView_button_openfolder);
  16.         openFolder.setVisibility(View.INVISIBLE);//设置不可见
  17.         openFolder.setOnClickListener(new buttonOnClickListener());//添加监听器
  18.         addSingle=(ImageButton) this.findViewById(R.id.MenuAddGridView_button_addSingle);
  19.         addSingle.setVisibility(View.INVISIBLE);
  20.         addSingle.setOnClickListener(new buttonOnClickListener());
  21.         addFolder=(ImageButton) this.findViewById(R.id.MenuAddGridView_button_addFolder);
  22.         addFolder.setVisibility(View.INVISIBLE);
  23.         addFolder.setOnClickListener(new buttonOnClickListener());
  24.         backFolder=(ImageButton) this.findViewById(R.id.MenuAddGridView_button_backFolder);
  25.         backFolder.setOnClickListener(new buttonOnClickListener());
  26.         
  27.         gridview=(GridView) this.findViewById(R.id.MenuAddGridView_gridview);
  28.         //设置gridView的数据
  29.         updategridviewAdapter(thisFilePath);
  30.         gridview.setOnItemClickListener(new OnItemClickListener(){
  31.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  32.                     long arg3) {
  33.                 HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);
  34.                 selectFilePath=(String) item.get("ItemFilePath");
  35.                
  36.                 if(item.get("type").equals("isDirectory"))//判断是否是文件夹
  37.                 {
  38.                     openFolder.setVisibility(View.VISIBLE);//打开文件按钮可见
  39.                     addSingle.setVisibility(View.INVISIBLE);//选择单曲按钮不可见
  40.                     addFolder.setVisibility(View.VISIBLE);//选择整个文件夹按钮可见
  41.                 }
  42.                 else if(item.get("type").equals("isMp3"))
  43.                 {
  44.                     openFolder.setVisibility(View.INVISIBLE);
  45.                     addSingle.setVisibility(View.VISIBLE);
  46.                     addFolder.setVisibility(View.INVISIBLE);
  47.                 }
  48.                 else
  49.                 {
  50.                     openFolder.setVisibility(View.INVISIBLE);
  51.                     addSingle.setVisibility(View.INVISIBLE);
  52.                     addFolder.setVisibility(View.INVISIBLE);
  53.                 }
  54.                
  55.             }});
  56.             this.setResult(0);
  57.     }
  58.     private File[] folderScan(String path)
  59.     {
  60.         File file=new File(path);
  61.         File[] files=file.listFiles();
  62.         return files;
  63.     }
  64.     //设置gridView的数据
  65.     private void updategridviewAdapter(String filePath)
  66.     {
  67.         File[] files=folderScan(filePath);
  68.         ArrayList<HashMap<String, Object>> lstImageItem = getFileItems(files);
  69.         gridviewAdapter = new SimpleAdapter(MenuAddGridView.this,lstImageItem,R.layout.menuaddgridview_item,new String[] {"ItemImage","ItemText"}, new int[] {R.id.MenuAddGridView_ItemImage,R.id.MenuAddGridView_ItemText});
  70.         gridview.setAdapter(gridviewAdapter);
  71.         gridviewAdapter.notifyDataSetChanged();
  72.     }
  73.     //列表循环判断文件类型然后提供数据给Adapter用
  74.     private ArrayList<HashMap<String, Object>> getFileItems(File[] files)
  75.     {
  76.         ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
  77.         //循环加入listImageItem数据
  78.         if(files==null)
  79.         {
  80.             return null;
  81.         }
  82.         for(int i=0;i<files.length;i++)
  83.         {
  84.             HashMap<String, Object> map = new HashMap<String, Object>();
  85.             String fileName=files[i].getName();//得到file名
  86.             map.put("ItemText", fileName);
  87.             if(files[i].isDirectory())//判断是否是文件夹
  88.             {
  89.                 map.put("ItemImage", R.drawable.folder);//显示文件夹图标
  90.                 map.put("type", "isDirectory");
  91.             }
  92.             else if(files[i].isFile())//判断是否是文件
  93.             {
  94.                 if(fileName.contains(".mp3"))//判断是否是MP3文件
  95.                 {
  96.                     map.put("ItemImage", R.drawable.mp3flie);//加入MP3图标
  97.                     map.put("type", "isMp3");
  98.                 }
  99.                 else
  100.                 {
  101.                     map.put("ItemImage", R.drawable.otherfile);//加入非MP3文件图标
  102.                     map.put("type", "isOthers");
  103.                 }
  104.             }
  105.             map.put("ItemFilePath", files[i].getAbsolutePath());//保存文件绝对路径
  106.             
  107.             lstImageItem.add(map);
  108.         }
  109.         return lstImageItem;
  110.     }
  111.     private ArrayList<String> getResultArrayList(ArrayList<HashMap<String, Object>> al)
  112.     {
  113.         ArrayList<String> musicResult=new ArrayList<String>();
  114.         for(int i=0;i<al.size();i++)
  115.         {
  116.             HashMap<String, Object> map=al.get(i);
  117.             String type=(String) map.get("type");
  118.             String itemFilePath=(String) map.get("ItemFilePath");
  119.             if(type.equals("isMp3"))
  120.             {
  121.                 musicResult.add(itemFilePath);
  122.             }
  123.         }
  124.         return musicResult;
  125.     }
  126.     class buttonOnClickListener implements OnClickListener
  127.     {
  128.         ArrayList<String> musicResult;
  129.         Intent intent;
  130.         public void onClick(View v) {
  131.             switch(v.getId())
  132.             {
  133.                 case R.id.MenuAddGridView_button_openfolder://打开文件夹
  134.                     updategridviewAdapter(selectFilePath);//获取文件夹下数据并显示
  135.                     thisFilePath=selectFilePath;//记录当前目录路径
  136.                     openFolder.setVisibility(View.INVISIBLE);
  137.                     addSingle.setVisibility(View.INVISIBLE);
  138.                     addFolder.setVisibility(View.INVISIBLE);
  139.                     break;
  140.                 case R.id.MenuAddGridView_button_addSingle:
  141.                     musicResult=new ArrayList<String>();
  142.                     musicResult.add(selectFilePath);
  143.                     intent=new Intent();
  144.                     intent.putStringArrayListExtra("musicResult", musicResult);
  145.                     MenuAddGridView.this.setResult(1, intent);
  146.                     MenuAddGridView.this.onDestroy();
  147.                     break;
  148.                 case R.id.MenuAddGridView_button_addFolder:
  149.                     //得到文件夹下所有mp3文件
  150.                     musicResult=getResultArrayList(getFileItems(folderScan(selectFilePath)));
  151.                     intent=new Intent();
  152.                     //返回给上一个activity数据
  153.                     intent.putStringArrayListExtra("musicResult", musicResult);
  154.                     MenuAddGridView.this.setResult(1, intent);
  155.                     MenuAddGridView.this.onDestroy();
  156.                     break;
  157.                 case R.id.MenuAddGridView_button_backFolder://返回上级目录
  158.                     if(!thisFilePath.equals(sdcardFilePath))
  159.                     {
  160.                         File thisFile=new File(thisFilePath);//得到当前目录
  161.                         String parentFilePath=thisFile.getParent();//上级的目录路径
  162.                         updategridviewAdapter(parentFilePath);//获得上级目录数据并显示
  163.                         thisFilePath=parentFilePath;//设置当前目录路径
  164.                         
  165.                         openFolder.setVisibility(View.INVISIBLE);
  166.                         addSingle.setVisibility(View.INVISIBLE);
  167.                         addFolder.setVisibility(View.INVISIBLE);
  168.                     }
  169.                     else
  170.                     {
  171.                         MenuAddGridView.this.onDestroy();
  172.                     }
  173.                     break;
  174.             }
  175.         }
  176.         
  177.     }
  178.     protected void onDestroy() {
  179.         this.finish();
  180.         super.onDestroy();
  181.     }
  182.    
  183. }
复制代码

效果图:




原创粉丝点击