此案例主要是模仿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. C#入门(面向对象概念)

    也许是看概念性的东西太多了,所以一看基本就明白,但实际并没有掌握,有待实操.反而是UML(统一建模语言)引起了我的兴趣,发现这东东很像建筑行业的图纸:有标准和约定,很方便专业人士看懂程序的架构和逻辑. ...

  2. 尚学堂Spring视频教程(三):Spring Core中的其他特性

    集合装配   如果bean中有一些集合属性,配置文件的配置如下 package com.bjsxt.dao.impl; import java.util.List; import java.util. ...

  3. SQLServer2008-镜像数据库实施手册(双机)SQL-Server2014同样适用

    SQL Server2008R2-镜像数据库实施手册(双机)SQL Server2014同样适用 一.配置主备机 1. 服务器基本信息 主机名称为:HOST_A,IP地址为:192.168.1.155 ...

  4. java.lang.IllegalArgumentException: template not initialized; call afterPropertiesSet() before using

    在使用spring-data-redis时使用junit测试报错: java.lang.IllegalArgumentException: template not initialized; call ...

  5. GMM的EM算法实现

    转自:http://blog.csdn.net/abcjennifer/article/details/8198352 在聚类算法K-Means, K-Medoids, GMM, Spectral c ...

  6. Webservice简介

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...

  7. Ninject之旅之三:Ninject对象生命周期

    摘要 DI容器的一个责任是管理他创建的对象的生命周期.他应该决定什么时候创建一个给定类型的对象,什么时候使用已经存在的对象.他还需要在对象不需要的时候处理对象.Ninject在不同的情况下管理对象的生 ...

  8. 28. Red Hat Linux安装Vmware Tools

    在VMware虚拟机中安装好了VMware Tools,才能实现主机与虚拟机之间的文件共享,同时可支持自由拖拽的功能,鼠标也可在虚拟机与主机之前自由移动(而不再用按ctrl+alT释放),而且还可以令 ...

  9. 8、SQL Server 表分区

    什么是表分区?表分区其实就是将一个大表分成若干个小表.表分区可以从物理上将一个大表分成几个小表,但是逻辑上还是一个表.所以当执行插入.更新等操作的时候,不需要我们去判断应该插入或更新到哪个表中.只需要 ...

  10. 自动获取MyEcilipse注册名和注册码的方法

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; //在MyEc ...