安卓--一个简单的SD卡文件浏览程序
2016-05-22 16:31
417 查看
一个简单的SD卡文件浏览程序。 本程序主要使用java.io中对于文件的一些操作,获取到手机上的SD卡之后,获取SD卡上的文件和文件夹,并将文件或文件夹的名字显示在ListView中,点击如果是文件夹,则显示该文件夹下面的文件,如果是文件,暂时不做任何操作,并且重写了点击返回键的方法。 java文件:
package com.example.zyf.sdcardfile; import android.content.DialogInterface; import android.os.Environment; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class BrowseFILES extends AppCompatActivity { TextView parentFile; Button upper; ListView fileList; //记录当前的父文件夹 File currentParent; //记录当前路径下的所有的文件数组 File[] currentFiles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_browse_files); parentFile=(TextView)findViewById(R.id.parentFile); upper=(Button)findViewById(R.id.upper); fileList=(ListView)findViewById(R.id.files); File root=new File("/mnt/sdcard/"); if (root.exists()) { currentParent = root; currentFiles = root.listFiles(); inflateListView(currentFiles); } fileList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //如果用户单击的是文件 if(currentFiles[position].isFile()){ Toast.makeText(BrowseFILES.this, "已经到达根目录", Toast.LENGTH_SHORT).show(); }else{ //用户单击的是文件夹 File[] temp=currentFiles[position].listFiles(); if(temp==null || temp.length==0){ Toast.makeText(BrowseFILES.this, "文件夹下没有文件或者当前路径不可访问", Toast.LENGTH_SHORT).show(); }else{ //将当前获取到的这个文件夹设为付文件夹 currentParent=currentFiles[position]; //保存当前父文件夹的全部文件和文件夹 currentFiles=temp; //在此更新ListView中要显示的内容 inflateListView(currentFiles); } } } }); //给上一级按钮添加事件 upper.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("文件路径"+currentParent.getAbsolutePath().toString()); if(currentParent.getAbsolutePath().toString().equals("/mnt/sdcard")){ return; }else{ //获取上一级目录 currentParent=currentParent.getParentFile(); //列出当前目录下所有文件 currentFiles=currentParent.listFiles(); //更新ListView inflateListView(currentFiles); } } }); } private void inflateListView(File[] files) { //创建集合,显示当前文件夹下所有的文件 List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>(); //给Map赋值 for(int i = 0;i<files.length;i++){ Map<String,Object> listItem=new HashMap<String, Object>(); //如果是文件夹,显示文件夹的图片 if(files[i].isDirectory()){ listItem.put("icon",R.mipmap.folder); }else{ listItem.put("icon",R.mipmap.file); } listItem.put("filename",files[i].getName()); listItems.add(listItem); } SimpleAdapter simple=new SimpleAdapter( this, listItems, R.layout.fileadapter, new String[]{"icon","filename"}, new int[]{R.id.fileImg,R.id.filename} ); fileList.setAdapter(simple); try { parentFile.setText(currentParent.getCanonicalPath().toString()); } catch (IOException e) { e.printStackTrace(); } } //重写点击返回键事件 @Override public void onBackPressed() { // super.onBackPressed(); if(currentParent.getAbsolutePath().toString().equals("/mnt/sdcard")){ //如果返回到根目录了,创建一个DiaLog AlertDialog.Builder alert=new AlertDialog.Builder(this); alert.setTitle("退出提示"); alert.setMessage("是否确认退出"); alert.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alert.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); alert.create(); alert.show(); }else{ currentParent=currentParent.getParentFile(); currentFiles=currentParent.listFiles(); inflateListView(currentFiles); } } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" 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="com.example.zyf.sdcardfile.BrowseFILES"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/parentFile" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="4" android:background="@android:drawable/edit_text" /> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/upper" android:text="上一层" /> </LinearLayout> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/files" /> </LinearLayout>
ListView的SimpleAdapter布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="40dp"> <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/fileImg" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="6" android:id="@+id/filename" /> </LinearLayout>
发现的问题: 问题探讨: 在对当前是否是根目录做比较的时候发现,调用getCanonicalPath() 不能准确的和给出的根目录做比较,所以,选择调用getAbsolutePath()方法,具体情况不明确。
相关文章推荐
- Go包管理gpm简介
- Unix程序跳转—setjmp与longjmp
- PPT1 例5
- 导航菜单背景切换效果
- 容器
- Rsync实现主服务器热备份
- Windows 系统实现VMware虚拟机下Ubuntu系统共享文件
- 深入了解LOD
- 216. Combination Sum III
- 关于ImageLoader继续研究
- glPushMatrix 与 glPopMatrix
- 用户登录账号、密码保存(懒人必备)
- JavaString的一些理解
- R语言 怎样导入带有“#”符号的数据集
- C++字符及字符串处理函数
- 第五次程序设计作业 更新
- [io PWA] keynote: Launching a Progressive Web App on Google.com
- 程序设计原则
- 滚动时动态加载页面内容
- 未来编程形式设想