资 源 简 介
[实例简介]开发mp4 [实例截图] [核心代码]package com.example.mp4;import java.io.File;import java.util.Vector;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;public class MyFileActivity extends Activity { private final String[] FILE_MapTable = { ".3gp", ".mov", ".avi", ".rmvb", ".wmv", ".mp3", ".mp4" }; private Vector items = null;// 存放显示的名称 private Vector paths = null;// 存放文件路径 private Vector sizes = null;// 存放文件大小 private String rootPath = "/mnt/sdcard";// 起始文件夹 private EditText pathEditText;// 路径 private Button queryButton;// 查询按钮 private ListView fileListView;// 文件列表 @Override protected void onCreate(Bundle icicle) { // TODO Auto-generated method stub super.onCreate(icicle); setContentView(R.layout.myfile); this.setTitle("多媒体文件浏览"); pathEditText = (EditText) findViewById(R.id.path_edit); queryButton = (Button) findViewById(R.id.qry_button); fileListView = (ListView) findViewById(R.id.file_listview); // 单击按钮事件 queryButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub File file = new File(pathEditText.getText().toString()); if (file.exists()) { if (file.isFile()) { // 如果是媒体文件直接打开 openFile(pathEditText.getText().toString()); } else { // 打开目录下的文件 getFileDir(pathEditText.getText().toString()); } } else { Toast.makeText(MyFileActivity.this, "找不到位置,请确定位置是否正确!", Toast.LENGTH_SHORT).show(); } } }); //设置listitem中的文件被单击时要做的动作 fileListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub fileOrDir(paths.get(position)); } }); //打开默认文件夹 getFileDir(rootPath); } //重写返回键功能是否为back public boolean onKeyDown(int keyCode,KeyEvent event){ //判断触发键是否为back键 if(keyCode == KeyEvent.KEYCODE_BACK){ pathEditText = (EditText)findViewById(R.id.path_edit); File file = new File(pathEditText.getText().toString()); if(rootPath.equals(pathEditText.getText().toString().trim())){ return super.onKeyDown(keyCode, event); }else{ getFileDir(file.getParent()); return true; } }else{ return super.onKeyDown(keyCode, event); } } //处理文件或目录的方法 private void fileOrDir(String path){ File file = new File(path); if(file.isDirectory()){ getFileDir(file.getPath()); }else{ openFile(path); } }//获取文件结构的方法 private void getFileDir(String filePath) { // TODO Auto-generated method stub pathEditText.setText(filePath); items = new Vector(); paths = new Vector(); sizes = new Vector(); File f = new File(filePath); File[] files = f.listFiles(); if (files != null) { /* 将所有文件添加到ArrayList中 */ for (int i = 0; i < files.length; i ) { if (files[i].isDirectory()) { items.add(files[i].getName()); paths.add(files[i].getPath()); sizes.add(""); } } for (int i = 0; i < files.length; i ) { if (files[i].isFile()) { String fileName = files[i].getName(); int index = fileName.lastIndexOf("."); if (index > 0) { String endName = fileName.substring(index, fileName.length()).toLowerCase(); String type = null; for(int x=0;i< FILE_MapTable.length;x ){ //符合预先定义的多媒体格式的文件才会在界面中显示 if(endName.equals(FILE_MapTable[x])){ type = FILE_MapTable[x]; break; } } if(type !=null){ items.add(files[i].getName()); paths.add(files[i].getPath()); sizes.add(files[i].length() ""); } } } } } fileListView.setAdapter(new FileListAdapter(this,items)); } private void openFile(String path) { // TODO Auto-generated method stub Intent intent = new Intent(MyFileActivity.this,MediaPlayerActivity.class); intent.putExtra("path", path); startActivity(intent); finish(); } //列表适配器 class FileListAdapter extends BaseAdapter{ private Vector items =null;//存放显示的名称 private MyFileActivity myFile; public FileListAdapter(MyFileActivity myFile,Vector items){ this.items = items; this.myFile = myFile; } @Override public int getCount() { // TODO Auto-generated method stub return items.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return items.elementAt(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return items.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if(convertView==null){ //加载列表布局文件 convertView = myFile.getLayoutInflater().inflate(R.layout.file_item, null); } //文件名称 TextView name = (TextView)convertView.findViewById(R.id.name); //媒体文件类型 ImageView music = (ImageView)convertView.findViewById(R.id.music); //文件夹类型 ImageView folder = (ImageView)convertView.findViewById(R.id.folder); name.setText(items.elementAt(position)); if(sizes.elementAt(position).equals("")){ //隐藏媒体文件图标,显示文件夹图标 music.setVisibility(View.GONE); folder.setVisibility(View.VISIBLE); }else{ music.setVisibility(View.VISIBLE); folder.setVisibility(View.GONE); } return convertView; } }}