zoukankan      html  css  js  c++  java
  • 文件选择器

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

    menuaddgridview.xml

    View Code
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
    ="vertical"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content">
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
    ="horizontal"
    android:layout_width
    ="match_parent"
    android:layout_height
    ="wrap_content"
    android:background
    ="@drawable/radiogroup_bg1"
    android:padding
    ="5.0dip">
    <ImageButton
    android:id="@+id/MenuAddGridView_button_openfolder"
    android:layout_width
    ="wrap_content" android:layout_height="wrap_content"
    android:layout_marginLeft
    ="10.0dip"
    android:src
    ="@drawable/radiogroup_openfolder"
    android:adjustViewBounds
    ="true"
    android:padding
    ="10.0dip"
    android:layout_alignParentLeft
    ="true"/>
    <ImageButton
    android:id="@+id/MenuAddGridView_button_addSingle"
    android:layout_width
    ="wrap_content" android:layout_height="wrap_content"
    android:layout_marginLeft
    ="10.0dip"
    android:src
    ="@drawable/radiogroup_addsingle"
    android:adjustViewBounds
    ="true"
    android:padding
    ="10.0dip"
    android:layout_toRightOf
    ="@id/MenuAddGridView_button_openfolder"/>
    <ImageButton
    android:id="@+id/MenuAddGridView_button_addFolder"
    android:layout_width
    ="wrap_content" android:layout_height="wrap_content"
    android:layout_marginLeft
    ="10.0dip"
    android:src
    ="@drawable/radiogroup_addfolder"
    android:adjustViewBounds
    ="true"
    android:padding
    ="10.0dip"
    android:layout_toRightOf
    ="@id/MenuAddGridView_button_addSingle"/>
    <ImageButton
    android:id="@+id/MenuAddGridView_button_backFolder"
    android:layout_width
    ="wrap_content" android:layout_height="wrap_content"
    android:layout_marginRight
    ="10.0dip"
    android:src
    ="@drawable/radiogroup_backfolder"
    android:adjustViewBounds
    ="true"
    android:padding
    ="10.0dip"
    android:layout_alignParentRight
    ="true"/>
    </RelativeLayout>
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id
    ="@+id/MenuAddGridView_gridview"
    android:layout_width
    ="match_parent"
    android:layout_height
    ="match_parent"
    android:numColumns
    ="auto_fit"
    android:verticalSpacing
    ="10dp"
    android:horizontalSpacing
    ="10dp"
    android:columnWidth
    ="90dp"
    android:stretchMode
    ="columnWidth"
    android:gravity
    ="center"
    />

    </LinearLayout>

    menuaddgridview_item.xml

    View Code
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height
    ="wrap_content"
    android:paddingBottom
    ="4dip" android:layout_width="fill_parent">
    <ImageView
    android:layout_height="wrap_content"
    android:id
    ="@+id/MenuAddGridView_ItemImage"
    android:layout_width
    ="wrap_content"
    android:layout_centerHorizontal
    ="true">
    </ImageView>
    <TextView
    android:layout_width="wrap_content"
    android:layout_below
    ="@id/MenuAddGridView_ItemImage"
    android:layout_height
    ="wrap_content"
    android:layout_centerHorizontal
    ="true"
    android:id
    ="@+id/MenuAddGridView_ItemText">
    </TextView>
    </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的代码:

    View Code
    public class MenuAddGridView extends Activity {
    GridView gridview;
    SimpleAdapter gridviewAdapter;
    ImageButton openFolder,addSingle,addFolder,backFolder;
    String sdcardFilePath,thisFilePath,selectFilePath;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
    this.setContentView(R.layout.menuaddgridview);

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

    openFolder
    =(ImageButton) this.findViewById(R.id.MenuAddGridView_button_openfolder);
    openFolder.setVisibility(View.INVISIBLE);
    //设置不可见
    openFolder.setOnClickListener(new buttonOnClickListener());//添加监听器
    addSingle=(ImageButton) this.findViewById(R.id.MenuAddGridView_button_addSingle);
    addSingle.setVisibility(View.INVISIBLE);
    addSingle.setOnClickListener(
    new buttonOnClickListener());
    addFolder
    =(ImageButton) this.findViewById(R.id.MenuAddGridView_button_addFolder);
    addFolder.setVisibility(View.INVISIBLE);
    addFolder.setOnClickListener(
    new buttonOnClickListener());
    backFolder
    =(ImageButton) this.findViewById(R.id.MenuAddGridView_button_backFolder);
    backFolder.setOnClickListener(
    new buttonOnClickListener());

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

    if(item.get("type").equals("isDirectory"))//判断是否是文件夹
    {
    openFolder.setVisibility(View.VISIBLE);
    //打开文件按钮可见
    addSingle.setVisibility(View.INVISIBLE);//选择单曲按钮不可见
    addFolder.setVisibility(View.VISIBLE);//选择整个文件夹按钮可见
    }
    else if(item.get("type").equals("isMp3"))
    {
    openFolder.setVisibility(View.INVISIBLE);
    addSingle.setVisibility(View.VISIBLE);
    addFolder.setVisibility(View.INVISIBLE);
    }
    else
    {
    openFolder.setVisibility(View.INVISIBLE);
    addSingle.setVisibility(View.INVISIBLE);
    addFolder.setVisibility(View.INVISIBLE);
    }

    }});
    this.setResult(0);
    }
    private File[] folderScan(String path)
    {
    File file
    =new File(path);
    File[] files
    =file.listFiles();
    return files;
    }
    //设置gridView的数据
    private void updategridviewAdapter(String filePath)
    {
    File[] files
    =folderScan(filePath);
    ArrayList
    <HashMap<String, Object>> lstImageItem = getFileItems(files);
    gridviewAdapter
    = new SimpleAdapter(MenuAddGridView.this,lstImageItem,R.layout.menuaddgridview_item,new String[] {"ItemImage","ItemText"}, new int[] {R.id.MenuAddGridView_ItemImage,R.id.MenuAddGridView_ItemText});
    gridview.setAdapter(gridviewAdapter);
    gridviewAdapter.notifyDataSetChanged();
    }
    //列表循环判断文件类型然后提供数据给Adapter用
    private ArrayList<HashMap<String, Object>> getFileItems(File[] files)
    {
    ArrayList
    <HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
    //循环加入listImageItem数据
    if(files==null)
    {
    return null;
    }
    for(int i=0;i<files.length;i++)
    {
    HashMap
    <String, Object> map = new HashMap<String, Object>();
    String fileName
    =files[i].getName();//得到file名
    map.put("ItemText", fileName);
    if(files[i].isDirectory())//判断是否是文件夹
    {
    map.put(
    "ItemImage", R.drawable.folder);//显示文件夹图标
    map.put("type", "isDirectory");
    }
    else if(files[i].isFile())//判断是否是文件
    {
    if(fileName.contains(".mp3"))//判断是否是MP3文件
    {
    map.put(
    "ItemImage", R.drawable.mp3flie);//加入MP3图标
    map.put("type", "isMp3");
    }
    else
    {
    map.put(
    "ItemImage", R.drawable.otherfile);//加入非MP3文件图标
    map.put("type", "isOthers");
    }
    }
    map.put(
    "ItemFilePath", files[i].getAbsolutePath());//保存文件绝对路径

    lstImageItem.add(map);
    }
    return lstImageItem;
    }
    private ArrayList<String> getResultArrayList(ArrayList<HashMap<String, Object>> al)
    {
    ArrayList
    <String> musicResult=new ArrayList<String>();
    for(int i=0;i<al.size();i++)
    {
    HashMap
    <String, Object> map=al.get(i);
    String type
    =(String) map.get("type");
    String itemFilePath
    =(String) map.get("ItemFilePath");
    if(type.equals("isMp3"))
    {
    musicResult.add(itemFilePath);
    }
    }
    return musicResult;
    }
    class buttonOnClickListener implements OnClickListener
    {
    ArrayList
    <String> musicResult;
    Intent intent;
    public void onClick(View v) {
    switch(v.getId())
    {
    case R.id.MenuAddGridView_button_openfolder://打开文件夹
    updategridviewAdapter(selectFilePath);//获取文件夹下数据并显示
    thisFilePath=selectFilePath;//记录当前目录路径
    openFolder.setVisibility(View.INVISIBLE);
    addSingle.setVisibility(View.INVISIBLE);
    addFolder.setVisibility(View.INVISIBLE);
    break;
    case R.id.MenuAddGridView_button_addSingle:
    musicResult
    =new ArrayList<String>();
    musicResult.add(selectFilePath);
    intent
    =new Intent();
    intent.putStringArrayListExtra(
    "musicResult", musicResult);
    MenuAddGridView.
    this.setResult(1, intent);
    MenuAddGridView.
    this.onDestroy();
    break;
    case R.id.MenuAddGridView_button_addFolder:
    //得到文件夹下所有mp3文件
    musicResult=getResultArrayList(getFileItems(folderScan(selectFilePath)));
    intent
    =new Intent();
    //返回给上一个activity数据
    intent.putStringArrayListExtra("musicResult", musicResult);
    MenuAddGridView.
    this.setResult(1, intent);
    MenuAddGridView.
    this.onDestroy();
    break;
    case R.id.MenuAddGridView_button_backFolder://返回上级目录
    if(!thisFilePath.equals(sdcardFilePath))
    {
    File thisFile
    =new File(thisFilePath);//得到当前目录
    String parentFilePath=thisFile.getParent();//上级的目录路径
    updategridviewAdapter(parentFilePath);//获得上级目录数据并显示
    thisFilePath=parentFilePath;//设置当前目录路径

    openFolder.setVisibility(View.INVISIBLE);
    addSingle.setVisibility(View.INVISIBLE);
    addFolder.setVisibility(View.INVISIBLE);
    }
    else
    {
    MenuAddGridView.
    this.onDestroy();
    }
    break;
    }
    }

    }
    protected void onDestroy() {
    this.finish();
    super.onDestroy();
    }

    }

    效果图:

  • 相关阅读:
    springcloud-Ribbon之手写轮询算法
    springcloud-Ribbon负载均衡规则的替换
    git本地库和远程库的连接和断开
    本地项目第一次提交到码云或github
    python基础语法练习
    Xss-labs-level11-15
    Vulnhub-靶机-ESCALATE_LINUX: 1
    Xss-labs-level7-10
    Vulnhub-靶机-DC: 6
    Jenkins入门之执行Powershell脚本
  • 原文地址:https://www.cnblogs.com/helloandroid/p/2149925.html
Copyright © 2011-2022 走看看