`

文件列表浏览的Demo

 
阅读更多
1.main.java文件:
Java代码 
package com.test;  
 
import java.io.File;  
import java.util.ArrayList;  
import java.util.List;  
import android.app.ListActivity;  
import android.content.Intent;  
import android.net.Uri;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.ListView;  
import android.widget.TextView;  
 
public class Main extends ListActivity {  
 
    /* 
     * 变量声明 items:存放显示的名称 paths:存放文件路径 rootPath:起始目录 
     */ 
    private List<String> items = null;  
    private List<String> paths = null;  
    private String rootPath = "/";  
    private TextView mPath;  
 
    protected void onCreate(Bundle icicle) {  
        super.onCreate(icicle);  
        setContentView(R.layout.main);  
        mPath = (TextView) findViewById(R.id.mPath);  
        getFileDir(rootPath);  
    }  
 
    /* 取得文件架构的方法 */ 
    private void getFileDir(String filePath) {  
        /* 设置目前所在路径 */ 
        mPath.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("b1");  
            paths.add(rootPath);  
            /* 第二笔设置为[回到上一层] */ 
            items.add("b2");  
            paths.add(f.getParent());  
        }  
        if (files != null) {  
            /* 将所有文件添加ArrayList中 */ 
            for (int i = 0; i < files.length; i++) {  
                File file = files[i];  
                items.add(file.getName());  
                paths.add(file.getPath());  
            }  
        }  
 
        /* 使用自定义的MyAdapter来将数据传入ListActivity */ 
        setListAdapter(new MyAdapter(this, items, paths));  
    }  
 
    /* 设置ListItem被点击时要做的动作 */ 
    protected void onListItemClick(ListView l, View v, int position, long id) {  
        File file = new File(paths.get(position));  
        if (file.isDirectory()) {  
            /* 如果是文件夹就再运行getFileDir() */ 
            getFileDir(paths.get(position));  
        } else {  
            /* 如果是文件就运行openFile() */ 
            openFile(file);  
        }  
    }  
 
    /* 在手机上打开文件的方法 */ 
    private void openFile(File f) {  
        Intent intent = new Intent();  
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        intent.setAction(android.content.Intent.ACTION_VIEW);  
 
        /* 调用getMIMEType()来取得MimeType */ 
        String type = getMIMEType(f);  
        /* 设置intent的file与MimeType */ 
        intent.setDataAndType(Uri.fromFile(f), type);  
        startActivity(intent);  
    }  
 
    /* 判断文件MimeType的方法 */ 
    private String getMIMEType(File f) {  
        String type = "";  
        String fName = f.getName();  
        /* 取得扩展名 */ 
        String end = fName  
                .substring(fName.lastIndexOf(".") + 1, fName.length())  
                .toLowerCase();  
 
        /* 依附档名的类型决定MimeType */ 
        if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")  
                || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {  
            type = "audio";  
        } else if (end.equals("3gp") || end.equals("mp4")) {  
            type = "video";  
        } else if (end.equals("jpg") || end.equals("gif") || end.equals("png")  
                || end.equals("jpeg") || end.equals("bmp")) {  
            type = "image";  
        } else {  
            /* 如果无法直接打开,就跳出软件列表给用户选择 */ 
            type = "*";  
        }  
        type += "/*";  
        return type;  
    }  


package com.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class Main extends ListActivity {

/*
* 变量声明 items:存放显示的名称 paths:存放文件路径 rootPath:起始目录
*/
private List<String> items = null;
private List<String> paths = null;
private String rootPath = "/";
private TextView mPath;

protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mPath = (TextView) findViewById(R.id.mPath);
getFileDir(rootPath);
}

/* 取得文件架构的方法 */
private void getFileDir(String filePath) {
/* 设置目前所在路径 */
mPath.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("b1");
paths.add(rootPath);
/* 第二笔设置为[回到上一层] */
items.add("b2");
paths.add(f.getParent());
}
if (files != null) {
/* 将所有文件添加ArrayList中 */
for (int i = 0; i < files.length; i++) {
File file = files[i];
items.add(file.getName());
paths.add(file.getPath());
}
}

/* 使用自定义的MyAdapter来将数据传入ListActivity */
setListAdapter(new MyAdapter(this, items, paths));
}

/* 设置ListItem被点击时要做的动作 */
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(paths.get(position));
if (file.isDirectory()) {
/* 如果是文件夹就再运行getFileDir() */
getFileDir(paths.get(position));
} else {
/* 如果是文件就运行openFile() */
openFile(file);
}
}

/* 在手机上打开文件的方法 */
private void openFile(File f) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);

/* 调用getMIMEType()来取得MimeType */
String type = getMIMEType(f);
/* 设置intent的file与MimeType */
intent.setDataAndType(Uri.fromFile(f), type);
startActivity(intent);
}

/* 判断文件MimeType的方法 */
private String getMIMEType(File f) {
String type = "";
String fName = f.getName();
/* 取得扩展名 */
String end = fName
.substring(fName.lastIndexOf(".") + 1, fName.length())
.toLowerCase();

/* 依附档名的类型决定MimeType */
if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
|| end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
type = "audio";
} else if (end.equals("3gp") || end.equals("mp4")) {
type = "video";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
|| end.equals("jpeg") || end.equals("bmp")) {
type = "image";
} else {
/* 如果无法直接打开,就跳出软件列表给用户选择 */
type = "*";
}
type += "/*";
return type;
}
}


2. MyAdapter.java适配器类:
Java代码 
 
package com.test;  
 
import java.io.File;  
import java.util.List;  
 
import android.content.Context;  
import android.graphics.Bitmap;  
import android.graphics.BitmapFactory;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.ImageView;  
import android.widget.TextView;  
 
/* 自定义的Adapter,继承android.widget.BaseAdapter */ 
public class MyAdapter extends BaseAdapter {  
 
    /* 
     * 变量声明 mIcon1:回到根目录的图文件 mIcon2:回到上一层的图档 mIcon3:文件夹的图文件 mIcon4:文件的图档 
     */ 
    private LayoutInflater mInflater;  
    private Bitmap mIcon1;  
    private Bitmap mIcon2;  
    private Bitmap mIcon3;  
    private Bitmap mIcon4;  
    private List<String> items;  
    private List<String> paths;  
 
    /* MyAdapter的构造器,传入三个参数 */ 
    public MyAdapter(Context context, List<String> it, List<String> pa) {  
        /* 参数初始化 */ 
        mInflater = LayoutInflater.from(context);  
        items = it;  
        paths = pa;  
        mIcon1 = BitmapFactory.decodeResource(context.getResources(),  
                R.drawable.back01);  
        mIcon2 = BitmapFactory.decodeResource(context.getResources(),  
                R.drawable.back02);  
        mIcon3 = BitmapFactory.decodeResource(context.getResources(),  
                R.drawable.folder);  
        mIcon4 = BitmapFactory.decodeResource(context.getResources(),  
                R.drawable.doc);  
    }  
 
    /* 因继承BaseAdapter,需覆盖以下方法 */ 
    public int getCount() {  
        return items.size();  
    }  
 
    public Object getItem(int position) {  
        return items.get(position);  
    }  
 
    public long getItemId(int position) {  
        return position;  
    }  
 
    public View getView(int position, View convertView, ViewGroup parent) {  
        ViewHolder holder;  
        if (convertView == null) {  
            /* 使用自定义的file_row作为Layout */ 
            convertView = mInflater.inflate(R.layout.file_row, null);  
            /* 初始化holder的text与icon */ 
            holder = new ViewHolder();  
            holder.text = (TextView) convertView.findViewById(R.id.text);  
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);  
            convertView.setTag(holder);  
        } else {  
            holder = (ViewHolder) convertView.getTag();  
        }  
 
        File f = new File(paths.get(position).toString());  
        /* 设置[回到根目录]的文字与icon */ 
        if (items.get(position).toString().equals("b1")) {  
            holder.text.setText("Back to /");  
            holder.icon.setImageBitmap(mIcon1);  
        }  
        /* 设置[回到上一层]的文字与icon */ 
        else if (items.get(position).toString().equals("b2")) {  
            holder.text.setText("Back to ..");  
            holder.icon.setImageBitmap(mIcon2);  
        } else {//设置[文件或文件夹]的文字与icon  
            holder.text.setText(f.getName());  
            if (f.isDirectory()) {  
                holder.icon.setImageBitmap(mIcon3);  
            } else {  
                holder.icon.setImageBitmap(mIcon4);  
            }  
        }  
        return convertView;  
    }  
 
    /* class ViewHolder */ 
    private class ViewHolder {  
        TextView text;  
        ImageView icon;  
    }  



package com.test;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/* 自定义的Adapter,继承android.widget.BaseAdapter */
public class MyAdapter extends BaseAdapter {

/*
* 变量声明 mIcon1:回到根目录的图文件 mIcon2:回到上一层的图档 mIcon3:文件夹的图文件 mIcon4:文件的图档
*/
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
private Bitmap mIcon3;
private Bitmap mIcon4;
private List<String> items;
private List<String> paths;

/* MyAdapter的构造器,传入三个参数 */
public MyAdapter(Context context, List<String> it, List<String> pa) {
/* 参数初始化 */
mInflater = LayoutInflater.from(context);
items = it;
paths = pa;
mIcon1 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.back01);
mIcon2 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.back02);
mIcon3 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.folder);
mIcon4 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.doc);
}

/* 因继承BaseAdapter,需覆盖以下方法 */
public int getCount() {
return items.size();
}

public Object getItem(int position) {
return items.get(position);
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
/* 使用自定义的file_row作为Layout */
convertView = mInflater.inflate(R.layout.file_row, null);
/* 初始化holder的text与icon */
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

File f = new File(paths.get(position).toString());
/* 设置[回到根目录]的文字与icon */
if (items.get(position).toString().equals("b1")) {
holder.text.setText("Back to /");
holder.icon.setImageBitmap(mIcon1);
}
/* 设置[回到上一层]的文字与icon */
else if (items.get(position).toString().equals("b2")) {
holder.text.setText("Back to ..");
holder.icon.setImageBitmap(mIcon2);
} else {//设置[文件或文件夹]的文字与icon
holder.text.setText(f.getName());
if (f.isDirectory()) {
holder.icon.setImageBitmap(mIcon3);
} else {
holder.icon.setImageBitmap(mIcon4);
}
}
return convertView;
}

/* class ViewHolder */
private class ViewHolder {
TextView text;
ImageView icon;
}
}


3.main.xml文件很简单:
Java代码 
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" android:background="@drawable/white">  
    <TextView android:id="@+id/mPath" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:padding="5px" 
        android:textSize="18sp" android:textColor="@drawable/blue">  
    </TextView>  
    <ListView android:id="@android:id/list" 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="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="@drawable/white">
<TextView android:id="@+id/mPath" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="5px"
android:textSize="18sp" android:textColor="@drawable/blue">
</TextView>
<ListView android:id="@android:id/list" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>


4.显示item的xml:
Java代码 
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="fill_parent">  
    <ImageView android:id="@+id/icon" android:layout_width="30dip" 
        android:layout_height="30dip">  
    </ImageView>  
    <TextView android:id="@+id/text" android:layout_gravity="center_vertical" 
        android:layout_width="0dip" android:layout_weight="1.0" 
        android:layout_height="wrap_content" android:textColor="@drawable/black">  
    </TextView>  
</LinearLayout> 
分享到:
评论

相关推荐

    MTP 文件浏览Demo

    Android MTP文件浏览。简单的Demo,可以浏览连接到当前设备上的MTP设备文件。

    文件在线预览Demo

    文件在线预览Demo 使用openoffice+swf+flexPaper实现 详细说明请见: http://blog.csdn.net/fei1502816/article/details/8906664

    Android自定义文件浏览器简单demo项目

    自定义的一个简单的文件浏览器的demo。是从根目录下开始浏览的。图标定义都有。留有接口,可以自己添加各个文件不同图标。

    图像文件格式转换 可浏览tiff图像 demo.rar

    图像文件格式转换 可浏览tiff图像

    html5 pdf浏览demo

    可以再浏览器中浏览PDF文件的完整项目。想要运行,只要打开web文件夹的viewer.html即可。可以打开本地PDF文件进行浏览。而且功能非常齐全。

    IE浏览器自动下载并运行.exe程序 demo

    IE浏览器自动下载并运行.exe程序 demo。具体介绍请查看博客:https://blog.csdn.net/qq_24484085/article/details/84785658

    PDF.js 解析PDF文件demo

    pdf.js 解析PDF文件DEMO 下载这个DEMO 轻松搞定在线解析PDF文件 支持翻页浏览 pdf.js 是一个技术原型主要用于在 HTML5 平台上展示 PDF 文档,无需任何本地技术支持 注意:只兼容支持HTML5的浏览器

    android中读取本地文件demo

    本demo实现了读取本地SD卡中的文件,word或者pdf,显示成一个listview中,可以打开浏览weord内容,可以进行搜索,搜索想要看的文件。

    PhotoSwipe4.1.1Demo-图片浏览插件仿微信朋友圈程序文件

    PhotoSwipe是一个图片放大插件,兼容pc和移动端,经历过多个版本的迭代且一直在不断更新,踩过的坑不知凡几,在移动端有着巨大的优势。 1、可控制多种风格如: ... 2、可支持移动端触摸手势兼容pc端 ...

    Android 基于x5Webview 浏览文件 office文档

    Android 基于x5Webview 浏览文件 office文档 已测试Android11 功能实现

    泛微E9建模demo应用-法务管理

    泛微E9系统建模demo, 法务管理,包含门户

    unity3d在浏览器上访问展示demo

    unity3d在浏览器上访问展示demo,unity3d web player demo。 包括内容:UnityWebPlayerFull.exe、UnityObject2.js、jquery.min.js、test.unity3d

    在线预览openOffice demo

    最近在要做在线预览,在网上找了方法,自己写的demo,亲测可用.实现方法,将office文件转换为pdf文件,浏览器可以直接在线查看pdf文件,就实现了在线预览.

    手机浏览本地文件(word版)

    手机浏览本地文件,主要应用JSR75包对手机本地文件进行读取

    磨砂玻璃demo文件.rar

    如果要在chrome或火狐浏览器查看,需要把index.html与图片文件放到本地的前端服务中,不然会出现Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin ...

    anuglar-route-demo

    angular1 route demo . 拷贝下载文件到web server下,例如tomcat的webapp下面,启动tomcat即可浏览。

    Android编程中File文件常见存储与读取操作demo示例

    主要介绍了Android编程中File文件常见存储与读取操作,结合实例形式分析了Android针对文件的打开、读写及布局等相关操作技巧,需要的朋友可以参考下

    良田高拍仪浏览器demo兼容chrome

    良田高拍仪兼容chrome进行操作的demo程序,程序需要安装对应的驱动,驱动包含在资源中。控件件支持IE,Chrome,FireFox三大主流浏览器内核,支持Win7,Win8,Win8.1及Win10等操作系统。注意:浏览器需要支持HTML5...

Global site tag (gtag.js) - Google Analytics