SD卡文件浏览器案例
来源:互联网 发布:mac os 10.13 开机慢 编辑:程序博客网 时间:2024/06/05 04:35
布局文件如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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" tools:context="com.example.sdcard_browser.MainActivity" android:orientation="vertical"> <TextView android:id="@+id/current_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true"/> <ListView android:id="@+id/file_browser" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/current_path"/> <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/back" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/></RelativeLayout>
Activity代码:
public class MainActivity extends AppCompatActivity { // 定义父文件 private File currentParent; // 存放当前父文件中的所有子文件 private File currentFiles[]; // 用于显示当前父文件的路径 private TextView textView; // 返回上一层按钮 private Button back; // 定义ListView用于加载文件列表 private ListView fileListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fileListView = (ListView) findViewById(R.id.file_browser); back = (Button) (Button) findViewById(R.id.back); textView = (TextView) findViewById(R.id.current_path); // 获取SD卡目录文件 File root = new File("/mnt/sdcard"); if(root.exists()){ currentParent = root; // listFiles()方法可得到当前文件下的所有文件 currentFiles = currentParent.listFiles(); filesInflateView(currentFiles); } fileListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(currentFiles[position].isFile()) return; File temp[] = currentFiles[position].listFiles(); if(temp == null || temp.length == 0){ Toast.makeText(MainActivity.this, "该文件夹为空或不可访问", Toast.LENGTH_SHORT).show(); }else{ currentParent = currentFiles[position]; currentFiles = temp; filesInflateView(currentFiles); } } }); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { // 若当前的父文件的目录不是SD卡目录 if(!currentParent.getCanonicalPath().equals("/storage/emulated/legacy")){ currentParent = currentParent.getParentFile(); currentFiles = currentParent.listFiles(); filesInflateView(currentFiles); } } catch (IOException e) { e.printStackTrace(); } } }); } private void filesInflateView(File currenFiles[]){ List<Map<String,Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < currenFiles.length;i++){ HashMap<String,Object> map = new HashMap<String,Object>(); if(currentFiles[i].isDirectory()){ map.put("icon",R.drawable.file); }else{ map.put("icon",R.drawable.sun_file); } map.put("fileName",currenFiles[i].getName()); listItems.add(map); } SimpleAdapter adapter = new SimpleAdapter(this,listItems,R.layout.file_item, new String[]{"icon","fileName"},new int[]{R.id.fileImage,R.id.fileName}); fileListView.setAdapter(adapter); try { textView.setText("当前路径:"+currentParent.getCanonicalPath()); } catch (IOException e) { e.printStackTrace(); } }}运行效果如下:
注:文件夹图片在windows上截的,哈哈~~~
0 0
- SD卡文件浏览器案例
- SD卡文件浏览器
- SD卡文件浏览器
- SD卡文件浏览器
- sd卡文件浏览器
- Android SD卡文件浏览器
- Android SD卡文件浏览器
- Android SD卡文件浏览器
- Android SD卡文件浏览器技术分析
- Android之SD卡文件浏览器
- 简单的SD卡文件浏览器
- Android之SD卡文件浏览器实现
- 实例:SD卡浏览器
- SD卡浏览器
- android SD卡浏览器
- SD卡文件夹浏览器
- Android应用小实例--SD卡文件浏览器
- Android的数据存储和IO-SD卡文件浏览器
- Office Skills
- 无法控制的并发问题
- HDU:2571 命运
- PyQT4文本框中文显示编码问题详解
- 基于GOLDENGATE实现数据实时分发
- SD卡文件浏览器案例
- leetcode 318. Maximum Product of Word Lengths
- CSRF 攻击的应对之道
- 非静态内部类可能导致的内存泄漏及其优化
- Dapper.NET使用入门(一)【LINQ2Dapper】
- 170.Jump Game II
- SSM框架+Log4j框架搭建
- iOS UILabel设置行间距和字间距并计算高度
- Linux常用命令总结