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

最近在做一个音乐播放器的小实例,其中涉及音乐文件选取。通过窗口模式的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. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
  9. this.setContentView(R.layout.menuaddgridview);

  10. sdcardFilePath=Environment.getExternalStorageDirectory().getAbsolutePath();//得到sdcard目录
  11. thisFilePath=sdcardFilePath;

  12. openFolder=(ImageButton) this.findViewById(R.id.MenuAddGridView_button_openfolder);
  13. openFolder.setVisibility(View.INVISIBLE);//设置不可见
  14. openFolder.setOnClickListener(new buttonOnClickListener());//添加监听器
  15. addSingle=(ImageButton) this.findViewById(R.id.MenuAddGridView_button_addSingle);
  16. addSingle.setVisibility(View.INVISIBLE);
  17. addSingle.setOnClickListener(new buttonOnClickListener());
  18. addFolder=(ImageButton) this.findViewById(R.id.MenuAddGridView_button_addFolder);
  19. addFolder.setVisibility(View.INVISIBLE);
  20. addFolder.setOnClickListener(new buttonOnClickListener());
  21. backFolder=(ImageButton) this.findViewById(R.id.MenuAddGridView_button_backFolder);
  22. backFolder.setOnClickListener(new buttonOnClickListener());

  23. gridview=(GridView) this.findViewById(R.id.MenuAddGridView_gridview);
  24. //设置gridView的数据
  25. updategridviewAdapter(thisFilePath);
  26. gridview.setOnItemClickListener(new OnItemClickListener(){
  27. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  28. long arg3) {
  29. HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);
  30. selectFilePath=(String) item.get("ItemFilePath");

  31. if(item.get("type").equals("isDirectory"))//判断是否是文件夹
  32. {
  33. openFolder.setVisibility(View.VISIBLE);//打开文件按钮可见
  34. addSingle.setVisibility(View.INVISIBLE);//选择单曲按钮不可见
  35. addFolder.setVisibility(View.VISIBLE);//选择整个文件夹按钮可见
  36. }
  37. else if(item.get("type").equals("isMp3"))
  38. {
  39. openFolder.setVisibility(View.INVISIBLE);
  40. addSingle.setVisibility(View.VISIBLE);
  41. addFolder.setVisibility(View.INVISIBLE);
  42. }
  43. else
  44. {
  45. openFolder.setVisibility(View.INVISIBLE);
  46. addSingle.setVisibility(View.INVISIBLE);
  47. addFolder.setVisibility(View.INVISIBLE);
  48. }

  49. }});
  50. this.setResult(0);
  51. }
  52. private File[] folderScan(String path)
  53. {
  54. File file=new File(path);
  55. File[] files=file.listFiles();
  56. return files;
  57. }
  58. //设置gridView的数据
  59. private void updategridviewAdapter(String filePath)
  60. {
  61. File[] files=folderScan(filePath);
  62. ArrayList<HashMap<String, Object>> lstImageItem = getFileItems(files);
  63. gridviewAdapter = new SimpleAdapter(MenuAddGridView.this,lstImageItem,R.layout.menuaddgridview_item,new String[] {"ItemImage","ItemText"}, new int[] {R.id.MenuAddGridView_ItemImage,R.id.MenuAddGridView_ItemText});
  64. gridview.setAdapter(gridviewAdapter);
  65. gridviewAdapter.notifyDataSetChanged();
  66. }
  67. //列表循环判断文件类型然后提供数据给Adapter用
  68. private ArrayList<HashMap<String, Object>> getFileItems(File[] files)
  69. {
  70. ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
  71. //循环加入listImageItem数据
  72. if(files==null)
  73. {
  74. return null;
  75. }
  76. for(int i=0;i<files.length;i++)
  77. {
  78. HashMap<String, Object> map = new HashMap<String, Object>();
  79. String fileName=files[i].getName();//得到file名
  80. map.put("ItemText", fileName);
  81. if(files[i].isDirectory())//判断是否是文件夹
  82. {
  83. map.put("ItemImage", R.drawable.folder);//显示文件夹图标
  84. map.put("type", "isDirectory");
  85. }
  86. else if(files[i].isFile())//判断是否是文件
  87. {
  88. if(fileName.contains(".mp3"))//判断是否是MP3文件
  89. {
  90. map.put("ItemImage", R.drawable.mp3flie);//加入MP3图标
  91. map.put("type", "isMp3");
  92. }
  93. else
  94. {
  95. map.put("ItemImage", R.drawable.otherfile);//加入非MP3文件图标
  96. map.put("type", "isOthers");
  97. }
  98. }
  99. map.put("ItemFilePath", files[i].getAbsolutePath());//保存文件绝对路径

  100. lstImageItem.add(map);
  101. }
  102. return lstImageItem;
  103. }
  104. private ArrayList<String> getResultArrayList(ArrayList<HashMap<String, Object>> al)
  105. {
  106. ArrayList<String> musicResult=new ArrayList<String>();
  107. for(int i=0;i<al.size();i++)
  108. {
  109. HashMap<String, Object> map=al.get(i);
  110. String type=(String) map.get("type");
  111. String itemFilePath=(String) map.get("ItemFilePath");
  112. if(type.equals("isMp3"))
  113. {
  114. musicResult.add(itemFilePath);
  115. }
  116. }
  117. return musicResult;
  118. }
  119. class buttonOnClickListener implements OnClickListener
  120. {
  121. ArrayList<String> musicResult;
  122. Intent intent;
  123. public void onClick(View v) {
  124. switch(v.getId())
  125. {
  126. case R.id.MenuAddGridView_button_openfolder://打开文件夹
  127. updategridviewAdapter(selectFilePath);//获取文件夹下数据并显示
  128. thisFilePath=selectFilePath;//记录当前目录路径
  129. openFolder.setVisibility(View.INVISIBLE);
  130. addSingle.setVisibility(View.INVISIBLE);
  131. addFolder.setVisibility(View.INVISIBLE);
  132. break;
  133. case R.id.MenuAddGridView_button_addSingle:
  134. musicResult=new ArrayList<String>();
  135. musicResult.add(selectFilePath);
  136. intent=new Intent();
  137. intent.putStringArrayListExtra("musicResult", musicResult);
  138. MenuAddGridView.this.setResult(1, intent);
  139. MenuAddGridView.this.onDestroy();
  140. break;
  141. case R.id.MenuAddGridView_button_addFolder:
  142. //得到文件夹下所有mp3文件
  143. musicResult=getResultArrayList(getFileItems(folderScan(selectFilePath)));
  144. intent=new Intent();
  145. //返回给上一个activity数据
  146. intent.putStringArrayListExtra("musicResult", musicResult);
  147. MenuAddGridView.this.setResult(1, intent);
  148. MenuAddGridView.this.onDestroy();
  149. break;
  150. case R.id.MenuAddGridView_button_backFolder://返回上级目录
  151. if(!thisFilePath.equals(sdcardFilePath))
  152. {
  153. File thisFile=new File(thisFilePath);//得到当前目录
  154. String parentFilePath=thisFile.getParent();//上级的目录路径
  155. updategridviewAdapter(parentFilePath);//获得上级目录数据并显示
  156. thisFilePath=parentFilePath;//设置当前目录路径

  157. openFolder.setVisibility(View.INVISIBLE);
  158. addSingle.setVisibility(View.INVISIBLE);
  159. addFolder.setVisibility(View.INVISIBLE);
  160. }
  161. else
  162. {
  163. MenuAddGridView.this.onDestroy();
  164. }
  165. break;
  166. }
  167. }

  168. }
  169. protected void onDestroy() {
  170. this.finish();
  171. super.onDestroy();
  172. }

  173. }
复制代码

效果图:

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

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

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




你可能感兴趣的:(android)