在使用ES文件浏览器的时候,当浏览SD卡下的文件或者其他地方的文件时,如果长按某一项文件或某一目录时会有"剪切"、"复制"、"重命名"、"删除" 等操作。于是乎自己也想弄一个类似与ES文件浏览器上面的拷贝复制功能。至于做一个类似ES文件浏览器 一样的软件,也是可以做的。
?
需要给 AndroidManifest.xml里加上权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>?
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
?
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | package com.copy.file; /** ? * @author wainiwann ? * Android SD卡文件目录拷贝操作 ? * ? */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; ? public class Copy_File extends Activity { ???? private Button m_btn = null ; ???? private final static String FROMPATH = "/mnt/sdcard/A/" ; ???? private final static String TOPATH = "/mnt/sdcard/B/" ; ? ???? /** Called when the activity is first created. */ ???? @Override ???? public void onCreate(Bundle savedInstanceState) ???? { ???????? super .onCreate(savedInstanceState); ???????? setContentView(R.layout.main); ???????? m_btn = (Button)findViewById(R.id.button1); ???????? m_btn.setOnClickListener( new OnClickListener() ???????? { ? ???????????? @Override ???????????? public void onClick(View v) ???????????? { ???????????????? // TODO Auto-generated method stub ???????????????? if (copy(FROMPATH, TOPATH)== 0 ) ???????????????? { ???????????????????? Toast.makeText(Copy_File. this , "文件拷贝成功!!!" , 20000 ).show(); ???????????????? } else ???????????????? { ???????????????????? Toast.makeText(Copy_File. this , "文件拷贝失败!!!" , 20000 ).show(); ???????????????? } ???????????? } ???????????????? ????????? }); ???? } ???? ????? public int copy(String fromFile, String toFile) ???? { ???????? //要复制的文件目录 ???????? File[] currentFiles; ???????? File root = new File(fromFile); ???????? //如同判断SD卡是否存在或者文件是否存在 ???????? //如果不存在则 return出去 ???????? if (!root.exists()) ???????? { ???????????? return - 1 ; ???????? } ???????? //如果存在则获取当前目录下的全部文件 填充数组 ???????? currentFiles = root.listFiles(); ???????? ????????? //目标目录 ???????? File targetDir = new File(toFile); ???????? //创建目录 ???????? if (!targetDir.exists()) ???????? { ???????????? targetDir.mkdirs(); ???????? } ???????? //遍历要复制该目录下的全部文件 ???????? for ( int i= 0 ;i<currentFiles.length;i++) ???????? { ???????????? if (currentFiles[i].isDirectory()) //如果当前项为子目录 进行递归 ???????????? { ???????????????? copy(currentFiles[i].getPath() + "/" , toFile + currentFiles[i].getName() + "/" ); ???????????????? ????????????? } else //如果当前项为文件则进行文件拷贝 ???????????? { ???????????????? CopySdcardFile(currentFiles[i].getPath(), toFile + currentFiles[i].getName()); ???????????? } ???????? } ???????? return 0 ; ???? } ???? ??? ????? //文件拷贝 ???? //要复制的目录下的所有非子目录(文件夹)文件拷贝 ???? public int CopySdcardFile(String fromFile, String toFile) ???? { ???????? ????????? try ???????? { ???????????? InputStream fosfrom = new FileInputStream(fromFile); ???????????? OutputStream fosto = new FileOutputStream(toFile); ???????????? byte bt[] = new byte [ 1024 ]; ???????????? int c; ???????????? while ((c = fosfrom.read(bt)) > 0 ) ???????????? { ???????????????? fosto.write(bt, 0 , c); ???????????? } ???????????? fosfrom.close(); ???????????? fosto.close(); ???????????? return 0 ; ???????????? ????????? } catch (Exception ex) ???????? { ???????????? return - 1 ; ???????? } ???? } ???? ?? } |
?下次在写个文件浏览器,然后在获取某一项的长按事件,然后弹出一个Dialog实现文件操作。