此案例主要是模仿QQ加载WPS(Word,Excel,PPT)本地文件可打开查看,使用ListView加载,使用线程扫描SD卡下所有目录加载指定的Word,Excel,PPT等格式的文件,ListView列表显示,点击Item则调用系统应用打开。

效果图:

代码:

public class MainActivity extends AppCompatActivity {

    public ProgressDialog dialog;
private ListView mListview;
private Context context;
private List<AddFileInfo> list=new ArrayList<AddFileInfo>();
private String filePath = Environment.getExternalStorageDirectory().toString() + File.separator;
private static Adapter adapter;
private ACache aCache;
private String fileDate="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mListview=(ListView) findViewById(R.id.listview);
context=this;
aCache=ACache.get(this);
onLoad();
}
public void onLoad() {
adapter=new Adapter(MainActivity.this);
String string=aCache.getAsString("file");
if(string==null)
{
showProgress();
new MyThread().start();
}else{
String[] str=string.split(","); for (int i=0;i<str.length;i++)
{
File f = new File(str[i]);
if(f.exists()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date(f.lastModified()));
AddFileInfo info = new AddFileInfo(f.getName(), Long.valueOf(fis.available()), time, false, f.getAbsolutePath());
fileDate += f.getAbsolutePath() + ",";
list.add(info);
} catch (Exception e) {
return;
}
}
}
}
mListview.setOnItemClickListener(onItemClickListener);
mListview.setAdapter(adapter);
} AdapterView.OnItemClickListener onItemClickListener=new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(OpenFile.openFile(list.get(position).getPath()));
}
}; public class MyThread extends Thread {
@Override
public void run() {
super.run();
try {
doSearch(filePath);
Thread.sleep(2000);
Message msg=new Message();
msg.what=1;
msg.obj=1;
handler.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
} }
} Handler handler=new Handler(){ @Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==1){
dismissProgress();
adapter.notifyDataSetChanged();
aCache.put("file",fileDate.substring(0,(fileDate.length()-1)),600);
}
}
}; /****
*计算文件大小
* @param length
* @return
*/
public static String ShowLongFileSzie(Long length)
{
if(length>=1048576)
{
return (length/1048576)+"MB";
}
else if(length>=1024)
{
return (length/1024)+"KB";
}
else if(length<1024) {
return length + "B";
}else{
return "0KB";
}
} /****
* 递归算法获取本地文件
* @param path
*/
private void doSearch( String path) {
File file = new File(path); if (file.exists()) {
if (file.isDirectory()) {
File[] fileArray = file.listFiles();
for (File f : fileArray) { if (f.isDirectory()) {
doSearch(f.getPath());
}
else {
if(f.getName().endsWith(".ppt") || f.getName().endsWith(".pptx") || f.getName().endsWith(".docx")
|| f.getName().endsWith(".xls") || f.getName().endsWith(".doc"))
{
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
String time=new SimpleDateFormat("yyyy-MM-dd").format(new Date(f.lastModified()));
AddFileInfo info=new AddFileInfo(f.getName(),Long.valueOf(fis.available()),time,false,f.getAbsolutePath());
list.add(info);
fileDate += f.getAbsolutePath() + ",";
System.out.println("文件名称:" + f.getName());
System.out.println("文件是否存在:" + f.exists());
System.out.println("文件的相对路径:" + f.getPath());
System.out.println("文件的绝对路径:" + f.getAbsolutePath());
System.out.println("文件可以读取:" + f.canRead());
System.out.println("文件可以写入:" + f.canWrite());
System.out.println("文件上级路径:" + f.getParent());
System.out.println("文件大小:" + f.length() + "B");
System.out.println("文件最后修改时间:" + new Date(f.lastModified()));
System.out.println("是否是文件类型:" + f.isFile());
System.out.println("是否是文件夹类型:" + f.isDirectory());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
} /***
* 启动
*/
public void showProgress()
{
if(dialog==null)
{
dialog=new ProgressDialog(MainActivity.this);
}
dialog.showMessage("正在加载");
} /***
* 关闭
*/
public void dismissProgress()
{
if(dialog==null)
{
dialog=new ProgressDialog(this);
}
dialog.dismiss();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}

不要忘记在AndroidManifest.xml加权限哦!

 <!-- SD卡权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
 

Android遍历获取Office格式(Word,Excel,PPT,PDF)的文件并打开的更多相关文章

  1. Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结

    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求 ...

  2. 在线文档转换API word,excel,ppt等在线文件转pdf、png

    在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...

  3. 微信小程序云开发-云存储-下载并打开文件文件(word/excel/ppt/pdf)

    一.wxml文件 1.写文本框,用来获取文件链接. 2.按钮,点击下载文件 <!-- 下载文件(word/excel/ppt/pdf等) --> <view class=" ...

  4. 微信小程序云开发-云存储-上传文件(word/excel/ppt/pdf)到云存储

    说明 word/excel/ppt/pdf是从客户端会话选择文件.使用chooseMessageFile中选择文件. 一.wxml文件 上传按钮,绑定chooseFile <!--上传文件(wo ...

  5. lucent检索技术之创建索引:使用POI读取txt/word/excel/ppt/pdf内容

    在使用lucent检索文档时,必须先为各文档创建索引.索引的创建即读出文档信息(如文档名称.上传时间.文档内容等),然后再经过分词建索引写入到索引文件里.这里主要是总结下读取各类文档内容这一步. 一. ...

  6. ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)

    Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架(支持当前主流的浏览器,且浏览器上无需安装任何插件,支持word.excel.ppt.pdf等文档 ...

  7. [转发]ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)

    转载自:https://www.cnblogs.com/Andre/p/9549874.html Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架 ...

  8. uploadify 下载组件使用技巧和在线预览 word,excel,ppt,pdf的方案

    http://www.cnblogs.com/wolf-sun/p/3565184.html uploadify 上传工具的使用技巧 http://www.cnblogs.com/wolf-sun/p ...

  9. 关于在线预览word,excel,ppt,pdf的需求处理方法。

    参考文档:http://www.cnblogs.com/wolf-sun/p/3574278.html 我选用的方案:先用office com组件生成pdf,然后使用pdf.js在线预览pdf文档.在 ...

随机推荐

  1. U-Boot GOT表分析和u-boot.lds解读

    转自:http://blog.sina.com.cn/s/blog_70dd16910100zab6.html u-boot-2010.09/arch/powerpc/cpu/mpc86xx/star ...

  2. delphi dev 汉化

    //把以下文件复制到记事本中,并保存为DevChs.ini放在exe的目录下 //有这个cxLocalizer控件 //主窗体创建的时候 if (fileexists(ExtractFilePath( ...

  3. virtualbox桥接网络配置--CentOS

    系统安装好后如下图设置virtualbox虚拟机的网络连接方式 然后启动虚拟机 ifconfig发现如下图 vi /etc/sysconfig/network-scripts/ifcfg-eth0 根 ...

  4. js 中文乱码解决方法

     bookManageAdd: function () {         top.MainFrameJS.confirm = true;         var action = getQueryS ...

  5. android开发--sqlite数据库

    一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQL ...

  6. equals标准写法

    @Overridepublic boolean equals(Object obj) { //为了提高效率 if(this == obj){ return true; } //为了提供程序的健壮性 / ...

  7. python学习:环境搭建

    1.图解eclipse环境下安装python3.x插件支持:http://www.tuicool.com/articles/M3Afyu 其中如果 然后,选择Add按钮,Name:Python3,Lo ...

  8. 浅谈 JS 创建对象的 8 种模式

    1.Object 模式 var o1 = {};//字面量的表现形式 var o2 = new Object; var o3 = new Object(); var o4 = new Object(n ...

  9. vertica 8.0 新特性

    前言: <<line>> 表明在vertica 8.0文档中的title   正文: 1-支持平台 1.1-操作系统 <<Vertica Server and Ve ...

  10. Oracle中SYS_CONNECT_BY_PATH函数的使用

    在Oracle中,SYS_CONNECT_BY_PATH函数主要作用是可以把一个父节点下的所有子节点通过某个字符进行区分,然后连接在一个列中显示. sys_connect_by_path(字段名, 2 ...