此案例主要是模仿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. Google Developing for Android 三 - Performance最佳实践

    Google Developing for Android 三 - Performance最佳实践 发表于 2015-06-07   |   分类于 Android最佳实践 原文 Developing ...

  2. Java里Serializable的那些事

    本文为原创文章,欢迎转载,但请注明出处http://www.cnblogs.com/yexiubiao/p/5014015.html,未在文章页面明显位置给出原文连接的,将保留追究法律责任的权利. 通 ...

  3. Manually enable Appear Offline in Lync 2013 Preview via Registry

    refer to http://www.shudnow.net/2012/09/18/manually-enable-appear-offline-in-lync-2013-preview-via-r ...

  4. 【HEVC】1、HM-16.7编码器的基本结构

    编码器在整个HM解决方案中的工程名为TAppEncoder,入口点函数位于encmain.cpp文件中: int main(int argc, char* argv[]) { TAppEncTop c ...

  5. thinkPHP环境搭建小记

    php一直以来都被人诟病,说什么设计得很糟糕,有种你别用啊,不然就别bb了.最近,森哥在去年暑假学习了php基础和mvc模式的基础上准备用尝试一下国产ThinkPHP框架. 1.搭建LAMP环境 我实 ...

  6. 静态static

    静态使用注意: 1.静态方法只能访问静态成员(方法和变量).非静态方法既可以访问静态也可以访问非静态. 2.静态方法中可以定义this,super关键字.因为静态优先于对象存在. 3. 主函数是静态的 ...

  7. 在MFC中使用GDI+的一般方法,以VC6.0编译器为例

    1.载解压GDI+开发包: 2.正确设置include & lib 目录: 设置如下:VC6.0编译器菜单Tools->Options->Directories中添加inlude ...

  8. 安卓工程 Installation error:INSTALL_PARSE_FAILED_MANIFEST_MALFORMED的解决办法

    解决办法:把包名首字母改成小写就好了.

  9. 用JS实现九九乘法表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. win10 自动亮度关闭无效问题

    升级win10后,发现系统的关闭自动亮度功能无效了,怎么调整都没有效果,百度把,服务也关了,电源管理也关了,自己的显卡节电设置也关了,最后摸索,只要把电池状态下,和通电状态下的屏幕亮度都调整到50%以 ...