Android编程实现简单文件浏览器功能
作者:村头那抠脚大叔 发布时间:2022-01-02 06:13:46
标签:Android,文件浏览器
本文实例讲述了Android编程实现简单文件浏览器功能。分享给大家供大家参考,具体如下:
运行效果:
布局:
<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="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
tools:context=".MainActivity" >
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/imageBt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home"/>
<ListView
android:id="@+id/listFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</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:orientation="horizontal" >
<ImageView
android:id="@+id/images"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
java代码:
package com.android.xiong.sdfilelook;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView listfile;
//当前文件目录
private String currentpath;
private TextView txt1;
private ImageView images;
private TextView textview;
private ImageButton imagebt1;
private int[] img = { R.drawable.file, R.drawable.folder, R.drawable.home };
private File[] files;
private SimpleAdapter simple;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listfile = (ListView) findViewById(R.id.listFile);
txt1 = (TextView) findViewById(R.id.txt1);
imagebt1 = (ImageButton) findViewById(R.id.imageBt1);
init(Environment.getExternalStorageDirectory());
listfile.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// 获取单击的文件或文件夹的名称
String folder = ((TextView) arg1.findViewById(R.id.txtview))
.getText().toString();
try {
File filef = new File(currentpath + '/'
+ folder);
init(filef);
} catch (Exception e) {
e.printStackTrace();
}
}
});
//回根目录
imagebt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
init(Environment.getExternalStorageDirectory());
}
});
}
// 界面初始化
public void init(File f) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 获取SDcard目录下所有文件名
files = f.listFiles();
if (!files.equals(null)) {
currentpath=f.getPath();
txt1.setText("当前目录为:"+f.getPath());
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < files.length; i++) {
Map<String, Object> maps = new HashMap<String, Object>();
if (files[i].isFile())
maps.put("image", img[0]);
else
maps.put("image", img[1]);
maps.put("filenames", files[i].getName());
list.add(maps);
}
simple = new SimpleAdapter(this, list,
R.layout.fileimageandtext, new String[] { "image",
"filenames" }, new int[] { R.id.images,
R.id.txtview });
listfile.setAdapter(simple);
}
} else {
System.out.println("该文件为空");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
权限控制:
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.csdn.net/x605940745/article/details/12580367
0
投稿
猜你喜欢
- 前言MyBatis-Plus 是基于 MyBatis 进行封装的一套优秀的持久层框架,它提供了丰富的便捷操作方法和强大的代码生成器,大大简化
- 1.导入 EasyExcel Maven包<!--easyexcel 导出excel依赖--><dependency>
- JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其
- 一、插入数据主键ID获取一般我们在做业务开发时,经常会遇到插入一条数据并使用到插入数据的ID情况。如果先插入在查询的话需要多一次sql查询,
- 一、jaxb是什么 JAXB是Java Architecture for XML Bindi
- 前言已经有两个月没有更新博客了,其实这篇文章我早在两个月前就写好了,一直保存在草稿箱里没有发布出来。原因是有一些原理性的东西还没了解清楚,最
- 如下所示:<span style="font-size:14px;">package com.imooc.r
- 目录一、String的用法1.构造方法2.求字符串长度和某一位置字符3.提取子串4.字符串比较5.字符串链接6.字符串中单个字符查找7.大小
- 本文实例为大家分享了Java实现酒店客房管理系统的具体代码,供大家参考,具体内容如下LoginFrame.javapackage login
- 前言上一篇文章已经介绍了fluent-mybatis项目的构建,文章地址:Java Fluent Mybatis实战之构建项目与代码生成篇上
- KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt提出的,因此人们称它为克努特—莫里斯—普
- 本文实例讲述了Java实现的不同图片居中剪裁生成同一尺寸缩略图功能。分享给大家供大家参考,具体如下:因为业务需要,写了这样一个简单类,希望能
- 目录简介强引用软引用弱引用虚引用简介从JDK1.2版本开始,把对象的引用分为四种级别,从而使程序能更加灵活的控制对象的生命周期。这四种级别由
- 一、前言在java中,和C语言一样,也有关于字符串的定义,并且有他自己特有的功能,下面我们一起来学习一下。二、String类概述string
- 本文实例讲述了Java实现Http工具类的封装操作。分享给大家供大家参考,具体如下:http工具类的实现:(通过apache包)第一个类im
- 本文中我们会讲解如何在Spring Boot JPA中实现class和数据表格的映射。默认实现Spring Boot JPA底层是用Hibe
- 当变换Java代码为Ceylon代码时,有时候我会遇到一些Java类构造器混淆了验证与初始化的情形。让我们使用一个简单但是人为的代码例子来说
- using (TransactionScope tr = new TransactionScope()) {  
- 页面:上传文件时的关键词:enctype="multipart/form-data"<%@ page langua
- 这篇文章主要介绍了Java开发工具IntelliJ IDEA安装图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习