您的位置:首页 > 移动开发 > Android开发

android:查找手机所有文件,进行浏览(文件浏览器)(13)

2016-01-24 00:00 525 查看
摘要: 查找手机所有文件,进行浏览(文件浏览器),主要方法getParentFile():获得父目录; MimeTypeMap.getFileExtensionFromUrl(files.getAbsolutePath()):获得文件的扩展名

//查找手机所有文件,进行浏览(文件浏览器)
public class MainActivity extends Activity {
private ListView listview;
private TextView showPath;
private SimpleAdapter adapter;
private File file;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.listview = (ListView) this.findViewById(R.id.listview);
this.showPath = (TextView) this.findViewById(R.id.showPath);
//获取sd卡中的根路径
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
file = new File(sdPath);
findFileFillListview(file);//查找文件的方法

}

public void findFileFillListview(File file) {
final File[] files = file.listFiles();
// 准备数据
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for (int i = 0; i < files.length; i++) {
Map<String, Object> fileMap = new HashMap<String, Object>();
if (files[i].isDirectory()) {
fileMap.put("fileImage", R.drawable.folder);// 目录的图标
fileMap.put("filename", files[i].getName());
}
if (files[i].isFile()) {
fileMap.put("fileImage", R.drawable.file);// 文件的图标
fileMap.put("filename", files[i].getName());// 这句有重复,可以把它提到外面去
}
// fileMap.put("filename", files[i].getName());
data.add(fileMap);
}
//适配器关联数据
adapter = new SimpleAdapter(this, data, R.layout.listviewitem,
new String[] { "fileImage", "filename" }, new int[] {
R.id.listview_image, R.id.listview_path });
listview.setAdapter(adapter);
showPath.setText(file.getAbsolutePath());
listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 点击可以查看文件,如果是目录,再进行判断
if (files[position].isDirectory()) {
File[] filesub = files[position].listFiles();
if (filesub.length == 0 || filesub == null) {
Toast.makeText(MainActivity.this, "该目录为空!", 200).show();
} else {
findFileFillListview(files[position]);
}
} else {// 如果是文件进行打开浏览
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
String extension = MimeTypeMap
.getFileExtensionFromUrl(files[position]
.getAbsolutePath());
// 获取文件的扩展名
if (extension.equals("jpg") || extension.equals("png")
|| extension.equals("png")
|| extension.equals("bmp")
|| extension.equals("gif")
|| extension.equals("jpeg")) {
intent.setDataAndType(Uri.fromFile(files[position]),
"image/*");

} else if (extension.equals("mp3")
|| extension.equals("png")
|| extension.equals("mp4")
|| extension.equals("mpg")
|| extension.equals("avi")
|| extension.equals("3gp")
|| extension.equals("mpeg")) {
intent.setDataAndType(Uri.fromFile(files[position]),
"video/*");
} else if (extension.equals("au")
|| extension.equals("mid")) {
intent.setDataAndType(Uri.fromFile(files[position]),
"video/*");

} else if (extension.equals("txt")
|| extension.equals("xml")
|| extension.equals("log")
|| extension.equals("html")) {
intent.setDataAndType(Uri.fromFile(files[position]),
"text/*");
} else {
intent.setDataAndType(Uri.fromFile(files[position]),
"text/*");
}
startActivity(intent);
}
}
});
}

public void checkButton(View view) {
if (!file.getAbsoluteFile().equals(Environment.getExternalStorageDirectory().getAbsolutePath())) {
findFileFillListview(file.getParentFile());
// 该按钮判断如果不等于绝对路径,获取父文件目录
}
/*
* switch (view.getId()) { case R.id.homeButtom: if
* (!file.getAbsoluteFile().equals(FileUtils.getSDCRRDDIR())) {
* findFileFillListview(file.getParentFile()); } break;
*
* }
*/
}
}
//主布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/showPath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:textColor="#fff"
android:textSize="20sp" />

<ImageView
android:id="@+id/homeButtom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="checkButton"
android:src="@drawable/home" />

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

//listview的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/listview_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/listview_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#88000000"
android:textColor="#fff"
android:textSize="20sp" />

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: