




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、http:/ Looper 實例,由于當(dāng)前線程是 UI 線程也可以通過 Looper.getMainLooper()得到messageHandler=newMessageHandler(Looper.myLooper();if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)rootDir=Environment.getExternalStorageDirectory();elserootDir=Environment.getRootDirectory();loadFiles(rootDir);自定義
2、 HandlerclassMessageHandlerextendsHandlerpublicMessageHandler(Looperlooper)super(looper);OverridepublicvoidhandleMessage(Messagemsg)loadFiles(newFile(currentDir.getText().toString();OverridepublicbooleanonCreateOptionsMenu(Menumenu)MenuInflaterinflater=getMenuInflater();inflater.inflate(R.menu.menu,
3、menu);returntrue;OverridepublicbooleanonOptionsItemSelected(MenuItemitem)if(item.getItemId()=R.id.newFile)LayoutInflaterfactory=LayoutInflater.from(MainActivity.this);finalViewview=factory.inflate(R.layout.rename,null);AlertDialogd=newAlertDialog.Builder(MainActivity.this).setCancelable(true).setMes
4、sage(文件夾名).setView(view).setPositiveButton(確定,newDialogInterface.OnClickListener()OverridepublicvoidonClick(DialogInterfacedialog,intwhich)StringdirName=(EditText)view.findViewById(R.id.rename).getText().toString();StringnewFile=currentDir.getText().toString()+/+dirName;if(newFile(newFile).exists()T
5、oast.makeText(MainActivity.this,”文件夾已存在”,Toast.LENGTH_LONG).show();return;Filef=newFile(currentDir.getText().toString(),dirName);f.mkdir();).create();d.show();elseif(item.getltemld()=R.id.about)Dialogd=newAlertDialog.Builder(MainActivity.this),setTitle(.setPositiveButtonf確定,null).create();d.show();e
6、lseif(item.getltemld()=R.id.exit)MainActivity.this.finish();)returntrue;)/*加載當(dāng)前文件夾列表*/publicvoidloadFiles(Filedir)ListMapString,Objectlist=newArrayListMapString,Object();if(dir!=null)/處理上級目錄if(!dir.getAbsolutePath().equals(rootDir.getAbsolutePath()Mapmap=newHashMap();文件瀏覽器 1.Obeta)setMessage(本程序由勁松
7、Alex 制作)map.put(file,dir.getParentFile();map.put(name,上一級目錄);map.put(img,R.drawable.folder);list.add(map);currentDir.setText(dir.getAbsolutePath();Filefiles=dir.listFiles();sortFiles(files);if(files!=null)for(Filef:files)MapString,Objectmap=newHashMap();map.put(file,f);map.put(name,f.getName();map.p
8、ut(img,f.isDirectory()?R.drawable.folder:(f.getName().toLowerCase().endsWith(.zip)?R.drawable.zip:R.drawable.text);list.add(map);elseToast.makeText(this,目錄不正確,請輸入正確的目錄!,Toast.LENGTH_LONG).show();ListAdapteradapter=newSimpleAdapter(this,list,R.layout.item,newStringname,img,newintR.,R.id.icon);
9、/listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);listView.setAdapter(adapter);)/* 排序文件列表* */privatevoidsortFiles(Filefiles)Arrays.sort(files,newComparator()publicintcompare(Filefilel,Filefile2)if(filel.isDirectoryO&file2.isDirectory()return1;if(file2.isDirectoryO)return1;return-1;);)/* 打開文
10、件* paramfile* /privatevoidopenFile(Filefile)Intentintent=newlntent();intent.addFlags(lntent.FLAG_ACTIVITY_NEW_TASK);/設(shè)置 intent 的 Action 屬性intent.setAction(lntent.ACTION_VIEW);/獲取文件 file 的 MIME 類型Stringtype=getMIMEType(file);/設(shè)置 intent 的 data 和 Type 屬性。intent.setDataAndType(Uri.fromFile(file),type);/
11、跳轉(zhuǎn)startActivity(intent);)/* 根據(jù)文件后綴名獲得對應(yīng)的 MIME 類型。* paramfile* /privateStringgetMIMEType(Filefile)Stringtype=*/*;StringfName=file.getName();/獲取后綴名前的分隔符在 fName 中的位置。intdotindex=fName.lastlndexOf(.);if(dotindex0)returntype;)/*獲取文件的后綴名*/Stringend=fName.substring(dotlndex,fName.length().toLowerCase();if(
12、end=)returntype;/在 MIME 和文件類型的匹配表中找到對應(yīng)的 MIME 類型。for(inti=0;iMIME_MapTable.length;i+)if(end.equals(MIME_MapTableiO)type=MIME_MapTablei1;)returntype;)OverridepublicvoidonClick(Viewv)if(v.getldQ=R.id.btnC)rootDir=Environment.getRootDirectory();loadFiles(rootDir);elseif(v.getId()=R.id.btnE)if(Environmen
13、t.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)rootDir=Environment.getExternalStorageDirectory();loadFiles(rootDir);OverridepublicvoidonItemClick(AdapterViewparent,Viewview,intposition,longid)MapString,Objectmap=(MapString,Object)parent.getItemAtPosition(position);finalFilefile=(File)m
14、ap.get(file);if(file.isDirectory()tryloadFiles(file);catch(Exceptione)Toast.makeText(this,權(quán)限不足,Toast.LENGTH_SHORT).show();elseopenFile(file);OverridepublicbooleanonItemLongClick(AdapterViewparent,Viewview,intposition,longid)Mapmap=(Map)parent.getItemAtPosition(position);finalFilefile=(File)map.get(f
15、ile);AlertDialogdialog=newAlertDialog.Builder(MainActivity.this).setTitle(操作).setItems(newString復(fù)制,剪切,粘貼,發(fā)送,重命名,刪除,屬性),newDialogInterface.OnClickListener()OverridepublicvoidonClick(DialogInterfacedialog,intwhich)switch(which)case0:copyPath=newFile(file.getAbsolutePath();flag=copy;break;case 1:copyPa
16、th=newFile(file.getAbsolutePath();flag=cut;break;case 2:finalStringstartPath=copyPath.getAbsolutePath();finalStringdesPath=currentDir.getText().toString()+/+copyPath.getName();Filefiles=newFile(currentDir.getText().toString().listFiles();for(Filefile:files)if(copyPath.getName().equals(file.getName()
17、Toast.makeText(MainActivity.this,此文件/文件夾已存在”,Toast.LENGTH_SHORT).show();return;)intlength=(int)(getLength(copyPath)/(1024);progressDialog=newProgressDialog(MainActivity.this);progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);progressDialog.setMessage(正在復(fù)制);progressDialog.setMax(length
18、);progressDialog.setProgress(0);progressDialog.setCancelable(false);progressDialog.show();newThread()publicvoidrun()copy(startPath,desPath);通過 Message 對象向原線程傳遞信息Messagemessage=Message.obtain();messageHandler.sendMessage(message);progressDialog.dismiss();/如果為剪切則刪除對應(yīng)文件/文件夾if(cut.equals(flag)if(copyPat
19、h.isFile()copyPath.delete();elseListdelDirs=delete(copyPath);for(inti=delDirs.size()-1;i=0;i-)delDirs.get(i).delete();.start();break;case 3:break;LayoutInflaterfactory=Layoutlnflater.from(MainActivity.this);finalViewview=factory.inflate(R.layout.rename,null);(EditText)view.findViewById(R.id.rename).
20、setText(file.getName();AlertDialogd=newAlertDialog.Builder(MainActivity.this).setCancelable(true).setMessage(新文件名).setView(view).setPositiveButton(確定,newDialogInterface.OnClickListener()OverridepublicvoidonClick(DialogInterfacedialog,intwhich)StringnewName=(EditText)view.findViewById(R.id.rename).ge
21、tText().toString();StringnewFile=currentDir.getText().toString()+/+newName;if(newFile(newFile).exists()Toast.makeText(MainActivity.this,文件名重復(fù)”,Toast.LENGTH_LONG).show();return;file.renameTo(newFile(newFile);).create();d.show();loadFiles(newFile(currentDir.getText().toString();break;AlertDialogad=new
22、AlertDialog.Builder(MainActivity.this).setMessage(確實要刪除+file.getName()+嗎?)setCancelable(true).setPositiveButton(確定,newDialogInterface.OnClickListener()OverridepublicvoidonClick(DialogInterfacedialog,intwhich)if(file.isFile()file.delete();elseListdelDirs=delete(file);for(inti=delDirs.size()-1;i=0;i-)
23、delDirs.get(i).delete();loadFiles(newFile(currentDir.getText().toString();).create();ad.show();break;default:break;Toast.makeText(MainActivity.this,+which,Toast.LENGTH_LONG).show();).create();dialog.show();/* Intentdata=newlntent();data.putExtra(path,* file.getAbsolutePath();this.setResult(RESULT_OK
24、,data);* this.finish();* /returntrue;)/*返回刪除文件后的空文件夾列表*/publicListdelete(FiledelFile)if(delFile.isFile()delFile.delete();elseif(delFile.isDirectoryO)fileList.add(delFile);Filefiles=delFile.listFiles();for(Filefile:files)delete(file);returnfileList;)/*獲取文件/文件夾大小*/privatelonggetLength(Filefile)if(file
25、.isDirectoryO)Filefiles=file.listFiles();for(Filefile2:files)totaLength=totaLength+getLength(file2);)elsetotaLength=totaLength+file.length();)returntotaLength;)/*復(fù)制功能,startFilePath 為文件源路徑,desFilePath 為目的路徑publicbooleancopy(StringstartFilePath,StringdesFilePath)currentLen=0;totaLength=0;this.startFil
26、ePath=startFilePath;this.desFilePath=desFilePath;/判斷是否返回成功的變量booleancopyFinished=false;FilestartFile=newFile(startFilePath);FiledesFile=newFile(desFilePath);/如果源文件是個文件if(startFile.isFile()copyFinished=this.copySingleFile(startFile,desFile);/如果源文件是個文件夾,就需要遞歸復(fù)制else/如果目標(biāo)文件夾是源文件夾的一個子目錄的情況,拒絕復(fù)制,因為會造成無限循環(huán)
27、if(desFilePath.startsWith(startFilePath)System.out.println(errorcopy);returnfalse;elsecopyFinished=this.copyFolder(startFile,desFile);)returncopyFinished;)/*復(fù)制單個文件,如果復(fù)制多個文件可以遞歸調(diào)用*/privatebooleancopySingleFile(FilestartSingleFile,FiledesSingleFile)booleanrightcopy=false;/從源文件中輸入內(nèi)存入 byte 中,在將 byte 寫入目
28、標(biāo)文件一FileinputstreamsingleFilelnputStream=null;DatalnputStreamsingleDatalnputStream=null;FileOutputStreamsingleFileOutputStream=null;DataOutputStreamsingleDataOutputStream=null;trysingleFilelnputStream=newFilelnputStream(startSingleFile);singleDataInputStream=newDataInputStream(newBufferedlnputStream
29、(singleFilelnputStream);singleFileOutputStream=newFileOutputStream(desSingleFile);singleDataOutputStream=newDataOutputStream(newBufferedOutputStream(singleFileOutputStream);byte 口 b=newbyte1024;intlen=-1;while(len=singleDataInputStream.read(b)!=-1)currentLen=currentLen+len;singleDataOutputStream.wri
30、te(b,0,len);progressDialog.setProgress(currentLen/(1024);/刷新緩沖區(qū)singleDataOutputStream.flush();catch(Exceptione)e.printStackTrace();finallytryif(singleDatalnputStream!=null)singleDatalnputStream.closeO;if(singleDataOutputStream!=null)singleDataOutputStream.closeO;catch(Exceptione)e.printStackTrace();
31、)/判斷源文件和目標(biāo)文件大小是否相同,如果相同證明復(fù)制成功if(startSingleFile.length()=desSingleFile.length()rightCopy=true;elserightCopy=false;returnrightCopy;)/*遞歸復(fù)制文件夾,因為文件夾下不止一個文件,里面可能有文件或文件夾,*paramstartFolder因此需要調(diào)用遞歸方法=需要復(fù)制的文件夾* paramdesFolder* =復(fù)制目的地的文件夾* return=true 表示成功,false 表示失敗* /publicbooleancopyFolder(FilestartFolde
32、r,FiledesFolder)booleanrightcopy=false;rightCopy=this.recursionCopy(startFolder,desFolder);returnrightCopy;)/* 復(fù)制文件夾函數(shù),此函數(shù)是個遞歸,會復(fù)制文件夾下的所有文件* paramrecFileFolder* =需要拷貝的文件夾或子文件夾* paramrecDesFolder* =拷貝的目的文件夾或子文件夾,* return=true 表示成功,false 表示失敗*/privatebooleanrecursionCopy(FilerecFileFolder,FilerecDesFo
33、lder)FiledesFolder=recDesFolder;/因為目的文件夾或子文件夾不存在,需要創(chuàng)建 desFolder.mkdir();/此為需要拷貝的文件夾下的所有文件列表(其中有文件和文件夾)Filefiles=recFileFolder.listFiles();/如果是個空文件夾if(files.length=0)returntrue;/*將文件夾下所有文件放入 for 循環(huán),如果是文件,那么調(diào)用 copySingleFile()拷貝文件,如果是文件夾,那么遞歸調(diào)用此函數(shù)*/for(FilethisFile:files)/如果此文件是個文件,那么直接調(diào)用單個文件復(fù)制命令復(fù)制文件
34、if(thisFile.isFile()/得到此文件的新位置地址StringdesContentFilePath=this.getSubFilePath(startFilePath,desFilePath,thisFile.getAbsolutePath();booleanrightCopy=this.copySingleFile(thisFile,newFile(desContentFilePath);/如果復(fù)制失敗,就跳出循環(huán)停止復(fù)制if(!rightCopy)returnfalse;/如果是個文件夾else/* 此函數(shù)是為了得到目的文件夾的地址,如:源文件夾為:D:/yingzi/tex
35、t(其中 text 文件夾下有另一個文件夾* second:D:/yingzi/text/second)目標(biāo)位置為:E:/aa/text* 那么此 second 文件夾在目標(biāo)地址的位置就是 E:/aa/text/second* /StringdesContentFilePath=this.getSubFilePath(startFilePath,desFilePath,thisFile.getAbsolutePath();/遞歸的調(diào)用此函數(shù),確保函數(shù)都被復(fù)制完全booleanrightCopy=recursionCopy(thisFile,newFile(desContentFilePath)
36、;if(!rightCopy)returnfalse;*D:/yingzi/text/second)目標(biāo)位置為:E:/aa/text 那么此 second 文件夾在目標(biāo)地址的位置就是* E:/aa/text/second 此方法中 startFolderPath=D:/yingzi/text(源文件夾);* desFolderPath=E:/aa/text(目標(biāo)位置);currentFilePath=* D:/yingzi/text/second(需要復(fù)制的子文件夾)返回值為:E:/aa/text/second*/privateStringgetSubFilePath(StringstartF
37、olderPath,StringdesFolderPath,StringcurrentFilePath)StringcurrentDesFilePath=null;inti=startFolderPath.length();/intj=desFolderPath.lastIndexOf(/);/StringsubDirPath=startFolderPath.substring(0,i);/StringsubDesPath=desFolderPath.substring(0,j);currentDesFilePath=desFolderPath+/+currentFilePath.substr
38、ing(i+1);/*)returntrue;此函數(shù)是為了得到目的文件夾的地址,如:源文件夾為:D:/yingzi/text(其中 text 文件夾下有另一個文件夾second:returncurrentDesFilePath;packagecom.mime;publicclassMIMEpublicstaticfinalStringMIME_MapTable=后綴名,MIME 類型.3gp,video/3gpp,.apk,application/vnd.android.package-archive,.asf,video/x-ms-asf,.avi,video/x-msvideo,.bin,application/octet-stream,.bmp,image/bmp,.c,text/plain,.class,application/octet-stream,.conf,text/plain,.cpp,text/plain,.doc,application/msword,.docx,application/vnd.openxmlf
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 大學(xué)助教培訓(xùn)心得體會范文
- 高校教師師德師風(fēng)建設(shè)意見學(xué)習(xí)心得體會
- 小學(xué)一年級運(yùn)動健康教育計劃
- 城市建設(shè)工程服務(wù)安全措施
- 金融服務(wù)擬投入主要物資計劃
- 學(xué)校師德師風(fēng)培訓(xùn)實施計劃
- 七年級信息化勞動技術(shù)教學(xué)計劃他
- 2025兒童口腔護(hù)理專項計劃
- 高一下學(xué)期年級部課外輔導(dǎo)計劃
- 手工制作興趣小組時尚設(shè)計活動計劃
- 《國家學(xué)生體質(zhì)健康標(biāo)準(zhǔn)》登記卡(高中樣表)
- 以硅的計算為例,比較S-W,Tersoff,MEAM勢的差異課件
- 折讓證明模板
- 培智學(xué)生XXX個別化教育手冊
- 初中化學(xué)講座課件
- 養(yǎng)老院管理運(yùn)營實施方案
- 勵磁系統(tǒng)試驗方案
- AWG,SWG,BWG線規(guī)規(guī)格對照表
- 廣州-樁機(jī)行走路線圖(共1頁)
- (整理)小學(xué)數(shù)獨(dú)游戲校本課程教材.
- KTV員工各種相關(guān)表樣(包括入職登記表)(共17頁)
評論
0/150
提交評論