Android遍历获取Office格式(Word,Excel,PPT,PDF)的文件并打开
此案例主要是模仿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)的文件并打开的更多相关文章
- Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结
Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word excel pdf 的web预览要求 ...
- 在线文档转换API word,excel,ppt等在线文件转pdf、png
在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...
- 微信小程序云开发-云存储-下载并打开文件文件(word/excel/ppt/pdf)
一.wxml文件 1.写文本框,用来获取文件链接. 2.按钮,点击下载文件 <!-- 下载文件(word/excel/ppt/pdf等) --> <view class=" ...
- 微信小程序云开发-云存储-上传文件(word/excel/ppt/pdf)到云存储
说明 word/excel/ppt/pdf是从客户端会话选择文件.使用chooseMessageFile中选择文件. 一.wxml文件 上传按钮,绑定chooseFile <!--上传文件(wo ...
- lucent检索技术之创建索引:使用POI读取txt/word/excel/ppt/pdf内容
在使用lucent检索文档时,必须先为各文档创建索引.索引的创建即读出文档信息(如文档名称.上传时间.文档内容等),然后再经过分词建索引写入到索引文件里.这里主要是总结下读取各类文档内容这一步. 一. ...
- ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架(支持当前主流的浏览器,且浏览器上无需安装任何插件,支持word.excel.ppt.pdf等文档 ...
- [转发]ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
转载自:https://www.cnblogs.com/Andre/p/9549874.html Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架 ...
- uploadify 下载组件使用技巧和在线预览 word,excel,ppt,pdf的方案
http://www.cnblogs.com/wolf-sun/p/3565184.html uploadify 上传工具的使用技巧 http://www.cnblogs.com/wolf-sun/p ...
- 关于在线预览word,excel,ppt,pdf的需求处理方法。
参考文档:http://www.cnblogs.com/wolf-sun/p/3574278.html 我选用的方案:先用office com组件生成pdf,然后使用pdf.js在线预览pdf文档.在 ...
随机推荐
- linux命令-cp/scp {拷贝}
一 命令解释 名称:cp 使用权限:所有使用者 使用方式: cp [options] source dest cp [options] source... directory 命令参数: -a 尽可能 ...
- WinForm控件TreeView 只部分节点显示 CheckBox
WinForm控件TreeView 只部分节点显示 CheckBox 用过asp.net的应该知道,要在treeview中实现上述功能可以使用ShowCheckBox 属性指定那些节点显示check ...
- 解决java文件编码和windows7系统(中文版)默认编码冲突所导致的乱码情况
开篇从一个比较简单但是也比较蛋疼的问题开始吧. 背景介绍:我是新手小白,初学java. 问题介绍:在使用UTF-8编码格式写java文件时,编译出现问题. 原因分析:1.java文件的编码格式是UTF ...
- workplace background
class:SysSetupFormRun public void run() { super(); this.design().colorScheme(FormColorScheme::RGB); ...
- VBA中如何动态定义数组
利用 dim Arr()as string这样声明,一旦赋值后,数组大小也就固定了.如果要改变数组大小,要用redim 命令redim arr(10) 加preserve 可以不清空数组,保持原有数据 ...
- smb:unrecognized service
[root@sycflash ~]#service smb status smb:unrecognized service 提示:smb:unrecognized service.这时你会按照常规的方 ...
- gulp.js基础入门
安装 Node 去 nodejs.org 根据系统选择性按照教程安装Node. 创建项目 创建项目文件夹 进入项目文件夹 初始化项目 使用npm命令:npm init,根据提示完成. 安装 Gulp ...
- JS不支持正则中的负向零宽断言
今天在项目中用到了正则表达式,并且需要用负向零宽断言 (?<=exp) 进行筛选,结果运行时报 Invalid group 错,一开始以为是自己很久没用表达式写错了,查阅了一下正则语法后发现并没 ...
- oracle contains
1. 查询住址在北京的学生SELECT student_id,student_nameFROM studentsWHERE CONTAINS( address, 'beijing' )remark: ...
- 【MySQL】编译安装
安装所需环境: yum install cmake make glibc gcc gcc-c++ libstdc++* sysstat lrzsz libtool libxml* libtool-lt ...