android文件选择、读取、浏览器的实现(音乐文件)

来源:互联网 发布:两点最短路径算法 编辑:程序博客网 时间:2024/04/29 15:22


关于android文件选择器的实现


小弟在此现丑了(实话)


主要的思路就是:

1.主界面显示的读取出来文件的ListView的列表

2.建立ListView的适配器

3.读取sdcard的文件

4.完成ListView的适配器的全部内容

5.事件响应,当有listview的点击事件,记录标号,对file的文件或者文件夹进行迭代遍历就OK啦


上代码+注释


main.xml文件(主界面的布局文件)

<?xml version="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" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
    
</LinearLayout>


FileChoiceDemoStudy1Activity(主ACTIVITY的界面文件)


ListView listView;//主界面的ListView,用于显示文件列表


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 列表框
listView = (ListView) findViewById(R.id.list);
// 设置数据适配器
final DataAdapter dataAdapter = new DataAdapter(this,
FileChoiceDemoStudy1Activity.this.getLayoutInflater());
listView.setAdapter(dataAdapter);
// 点击事件
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
dataAdapter.UpdateView(arg2);//更新界面(哎、方法名没有取规范)
dataAdapter.notifyDataSetChanged();(通知适配器刷新)
}
});
}


DataAdapter(listView的数据适配器


public class DataAdapter extends BaseAdapter
{

String path;                                                            //路径
String musicType[] = {"mp3","wav"};                     //识别声音文件
Context context;//上下文
ArrayList<String> list = new ArrayList<String>(); //遍历的文件列表
LayoutInflater layoutInflater;                               //布局容器

public DataAdapter(Context context , LayoutInflater layoutInflater)
{
this.context = context;
this.layoutInflater = layoutInflater;

//初始化读取sdcard数据
File file = new File("/mnt/sdcard/");
File files[];
if( file.isDirectory() )
{
//获得文件
files = file.listFiles();
//获得文件的名字
for( int i =0 ; i < files.length ; i++ )
{
//加载到数据集中
System.out.println("init " + files[i].getName() );
list.add(files[i].getAbsolutePath());
}
}
}

//更新界面的方法
public void UpdateView(int id)
{
System.out.println("is : " + list.get(id));

//如果是音乐文件的话,就播放。如果不是就提示不是音乐文件。如果是文件夹的话就在进一步
File choiceFile = new File(list.get(id)); 

if( choiceFile.isDirectory() )
{
//如果是文件夹的话就进步深入
list.removeAll(list);
System.out.println( "size="+ list.size());
//重新载入
File files[] = choiceFile.listFiles();
for( int i =0 ; i < files.length ; i++ )
{
//加载到数据集中
list.add(files[i].getAbsolutePath());
}
System.out.println("size="+ list.size());
}
else 
{
//是其他的文件
String fileNmae = choiceFile.getName();
int len =  fileNmae.lastIndexOf(".");
String hz = fileNmae.substring(len+1);
System.out.println("hz = " + hz);
for( int i = 0 ; i <  musicType.length ; i++)
{
if( hz.equals(musicType[i]) )
{
//是音乐文件
MediaPlayer player = new MediaPlayer();
try 
{
player.setDataSource(choiceFile.getAbsolutePath());
player.prepare();
player.start();
}  
catch (Exception e) 
{
e.printStackTrace();
     }
return;
}//if
}//for
System.out.println("这个不是音乐文件!");
}
}

public int getCount() 
{
return list.size();
}


public Object getItem(int arg0) 
{
return list.get(arg0);
}


public long getItemId(int arg0) 
{
return arg0;
}


public View getView(int arg0, View arg1, ViewGroup arg2) 
{
TextView textView = new TextView(context);
textView.setText(list.get(arg0));
return textView;
}


}


好了,就这么写就可以完成文件的读取功能啦,而且可以实现读取你想要的格式文件


如果需要代码给我留言哟~


对了,注意有2个缺陷,

1.没有对sdcard的存在进行一个判断,如果没有的话,程序可能崩溃

2.读取sdcard的路径可能不对,因为我是按照我的模拟器的上面的路径写的路径,但是android手机变化多段,所以不同的手机sdcard的路径可能不一致,如果出现问题的话,

就用这句话,就OK啦、、

       Environment.getExternalStorageDirectory()

      还有添加权限:

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


原创粉丝点击