Android遍歷獲取Office格式(Word,Excel,PPT,PDF)的檔案並開啟
阿新 • • 發佈:2019-02-08
此案例主要是模仿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" />