1 /** 2 * copy data/data/包名/files 文件夹下的文件到本地sdcard根目录teddyData_files目录下 3 */ 4 public boolean copyFileToSdcard() { 5 boolean state = false; 6 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 7 File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), File.separator + "teddy_files_data"); 8 TedSDKLog.d(TAG, "sdcardFilePath" + file.getAbsolutePath()); 9 10 state = copyFolder(context.getFilesDir().getAbsolutePath(), file.getAbsolutePath()); 11 }else{ 12 } 13 return state; 14 } 15 16 17 18 /** 19 * 复制整个文件夹内容 20 * @param oldPath String 原文件路径 如:c:/fqf 21 * @param newPath String 复制后路径 如:f:/fqf/ff 22 * @return boolean 23 */ 24 public boolean copyFolder(String oldPath, String newPath) { 25 boolean state = false; 26 try { 27 File newFile = new File(newPath); 28 if(newFile.exists()){ 29 newFile.delete(); 30 } 31 newFile.mkdir(); //如果文件夹不存在 则建立新文件夹 32 File oldFile = new File(oldPath); 33 String[] file = oldFile.list(); 34 File temp = null; 35 for (int i = 0; i < file.length; i++) { 36 if(oldPath.endsWith(File.separator)){ 37 temp = new File(oldPath+file[i]); 38 } 39 else{ 40 temp = new File(oldPath+File.separator+file[i]); 41 } 42 43 if(temp.isFile()){ 44 FileInputStream input = new FileInputStream(temp); 45 FileOutputStream output = new FileOutputStream(newPath + "/" + 46 (temp.getName()).toString()); 47 byte[] buf = new byte[1024 * 5]; 48 int len; 49 while ( (len = input.read(buf)) != -1) { 50 output.write(buf, 0, len); 51 } 52 output.flush(); 53 output.close(); 54 input.close(); 55 } 56 if(temp.isDirectory()){//如果是子文件夹 57 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); 58 } 59 } 60 state = true; 61 } 62 catch (Exception e) { 63 if(Debug.DEBUG) { 64 TedSDKLog.d(TAG, e.getMessage()); 65 TedSDKLog.d(TAG, "复制整个文件夹内容操作出错"); 66 e.printStackTrace(); 67 } 68 state = false; 69 } 70 return state; 71 }