简单的SD卡文件浏览器

<pre name="code" class="html">
package com.example.filedemo;/*SD卡文件浏览器 * 需要添加权限: * <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> * */import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Button;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;import android.app.Activity;public class MainActivity extends Activity {TextView t1;ListView l1;Button back;//记录当前的父文件夹File currentParent;//记录当前路劲下的所有文件的文件数组File[] currentFiles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); t1=(TextView) findViewById(R.id.t1); l1=(ListView) findViewById(R.id.l1); back=(Button) findViewById(R.id.back); //获取系统的SD卡的目录 File root=new File("/mnt/sdcard"); //如果SD卡存在 if(root.exists()){ currentParent=root; currentFiles=root.listFiles(); //将当前路径下面所有的文件、文件夹填充到listview inflatelateListView(currentFiles); } //为ListView的列表项的单击事件绑定监听器 l1.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {// TODO Auto-generated method stub//假如点击的是个文件,直接返回if(currentFiles[position].isFile())return;//获取点击文件夹下面的所有文件、文件夹File[] tmp=currentFiles[position].listFiles();if((tmp==null)||(tmp.length==0)){Toast.makeText(MainActivity.this,"当前路径不可访问或该路径下没有文件",Toast.LENGTH_SHORT).show();}else{//获取用户单击的列表项对应的文件夹,设为当前的父文件夹currentParent=currentFiles[position];//保存当前的父文件夹内的全部文件和文件夹currentFiles=tmp;//再次更新ListViewinflatelateListView(currentFiles);}}}); //获取上一级目录的按钮 back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry {if(!currentParent.getCanonicalPath().equals("/mnt/sdcard")){//获取上一级目录currentParent=currentParent.getParentFile();//列出当前目录下所有文件currentFiles=currentParent.listFiles();//更新LisetViewinflatelateListView(currentFiles);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}); }private void inflatelateListView(File[] file) {// TODO Auto-generated method stub//创建一个List集合,List集合的元素是MapList<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();for(int i=0;i<file.length;i++){Map<String,Object> item=new HashMap<String, Object>();if(file[i].isDirectory()){//当前File是文件夹item.put("icon",R.drawable.folder);}else{item.put("icon", R.drawable.file);}item.put("filename",file[i].getName());listItems.add(item);}//为ListView设置AdapterSimpleAdapter simpleadapter=new SimpleAdapter(this,listItems,R.layout.line,new String[]{"icon","filename"},new int[]{R.id.icon,R.id.file_name});l1.setAdapter(simpleadapter);try {t1.setText("当前路径为:"+currentParent.getCanonicalPath());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ListView
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="358dp" >

    </ListView>

    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/file_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:text="TextView" />

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值