<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:id="@+id/TextView"
android:background="#ffff00"
android:layout_height="wrap_content" android:text="@string/hello" />
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
package com.Aina.Android;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Test_ListFile extends ListActivity {
/** Called when the activity is first created. */
private List<String> items = null;//存放名称
private List<String> paths = null;//存放路径
private String rootPath = "/";
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.TextView);
this.getFileDir(rootPath);//获取rootPath目录下的文件.
}
public void getFileDir(String filePath) {
try{
this.tv.setText("当前路径:"+filePath);// 设置当前所在路径
items = new ArrayList<String>();
paths = new ArrayList<String>();
File f = new File(filePath);
File[] files = f.listFiles();// 列出所有文件
// 如果不是根目录,则列出返回根目录和上一目录选项
if (!filePath.equals(rootPath)) {
items.add("返回根目录");
paths.add(rootPath);
items.add("返回上一层目录");
paths.add(f.getParent());
}
// 将所有文件存入list中
if(files != null){
int count = files.length;// 文件个数
for (int i = 0; i < count; i++) {
File file = files[i];
items.add(file.getName());
paths.add(file.getPath());
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
this.setListAdapter(adapter);
}catch(Exception ex){
ex.printStackTrace();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String path = paths.get(position);
File file = new File(path);
//如果是文件夹就继续分解
if(file.isDirectory()){
this.getFileDir(path);
}else{
new AlertDialog.Builder(this).setTitle("提示").setMessage(file.getName()+" 是一个文件!").setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}
}
分享到:
相关推荐
原数据存放在StreamingAsset中,首次启动复制到persistentDataPath,以后进行更新和读取都在persistentDataPath中使用File进行文件操作。需要恢复书序的时候从StreamingAsset中获取即可。
android实际开发中遇到的关于文件操作方面的总结,以word文档形式展现出来。个人认为比较全面~
打开Android Studio中的File下的Settings 选中Version Control下的Ignored Files 添加对应的需要忽略的文件 Ignore Unversioned Files下有三种忽略文件方式 Ignore specified file 忽略指定文件 Ignore ...
NULL 博文链接:https://z251257144.iteye.com/blog/1355245
//返回上一层目录的操作 public void backProcess(){ //判断当前路径是不是sdcard路径 , 如果不是,则返回到上一层。 if (!mLastFilePath.equals(mSdcardRootPath)) { File thisFile = new File...
[android]测试文件操作的小程序,了解android中如何对file进行操作
android文件操作封装的java类,包括根据关键字查找文件,遍历目录,删除目录所有文件等等
对话框以列表形式显示文件目录 选择文件完成后返回文件的路径
Android中存储文件的一种方式,请大家遵循开发者规范,避免大量垃圾资源!
Android-android-file-transfer-linux.zip,Android Linux文件传输,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
EAsy File Explorer文件管理器是一款功能强大的本地、蓝牙文件管理器. EY文件管理器主要功能有: 1)管理本地手机上的文件、支持蓝牙共享,邮件发送等更多共享方式; 2)自适应所有分辨率手机、MID等ANDROID 便携...
该压缩包中含有两个android工程:file和other。file中主要讲述了对文件的保存与读取的功能,other主要用来测试文件保存的四种操作模式。
Android File(文件/目录)常见操作的封装:创建文件、删除单个文件、删除目录、获得文件大小、获得目录大小、拷贝单个文件、拷贝目录、剪切单个文件、剪切目录等
android 9.0及其以上版本手机 操作数据库,再打开数据库db文件出现 “File opened that is not a database file file is encrypted or is not a database”android 9.0以下的版本 不会出现。 原因及解决方法。
android 下的文件读写操作源码,可供入门学习
这是一个关于异步操作,讲解onPreExecute,doInBackground等方法
讲述如何将文件复制到安卓模拟器的方法.里面包含两种方法,一种是在Android Stuido 里面操作,一种是在命令行里操作.
Android 文件操作 ,文件的创建 ,数据写入,数据删除
主要介绍了Android编程中File文件常见存储与读取操作,结合实例形式分析了Android针对文件的打开、读写及布局等相关操作技巧,需要的朋友可以参考下