• Android里面copy资源文件到目标目录中


    1. private File getFilePtr(String outName,String subFolder) throws IOException {
    2. //找到目录
    3. File filesDir = getApplicationContext().getFilesDir();
    4. if (!filesDir.exists()) {
    5. filesDir.mkdirs();
    6. }
    7. //创建专属目录
    8. File outFileFolder = new File(filesDir.getAbsolutePath()+"/target/"+subFolder);
    9. if(!outFileFolder.exists()) {
    10. outFileFolder.mkdirs();
    11. }
    12. //创建输出文件
    13. File outFile=new File(outFileFolder,outName);
    14. String outFilename = outFile.getAbsolutePath();
    15. Log.i(TAG, "outFile is " + outFilename);
    16. if (!outFile.exists()) {
    17. boolean res = outFile.createNewFile();
    18. if (!res) {
    19. Log.e(TAG, "outFile not exist!(" + outFilename + ")");
    20. return null;
    21. }
    22. }
    23. return outFile;
    24. }
    25. private int copyData(File outFile,InputStream is){
    26. try {
    27. FileOutputStream fos = new FileOutputStream(outFile);
    28. //分段读取文件,并写出到输出文件,完成拷贝操作。
    29. byte[] buffer = new byte[1024];
    30. int byteCount;
    31. while ((byteCount = is.read(buffer)) != -1) {
    32. fos.write(buffer, 0, byteCount);
    33. }
    34. fos.flush();
    35. is.close();
    36. fos.close();
    37. return 0;
    38. } catch (Exception e) {
    39. e.printStackTrace();
    40. }
    41. return -1;
    42. }
    43. public String getFilePathAfterCopy(Uri uri,String outName,String subFolder,boolean ifReturnParent){
    44. try {
    45. File outFile=getFilePtr(outName,subFolder);
    46. //创建输入文件流
    47. InputStream is= this.getContentResolver().openInputStream(uri);
    48. if(0!=copyData(outFile,is)) {
    49. return null;
    50. }
    51. //返回路径
    52. if(ifReturnParent) {
    53. return outFile.getParent();
    54. } else {
    55. return outFile.getPath();
    56. }
    57. } catch (Exception e) {
    58. e.printStackTrace();
    59. }
    60. return null;
    61. }
    62. public String getFilePathAfterCopy(int resId,String outName,String subFolder,boolean ifReturnParent) {
    63. try {
    64. //找到目录
    65. File outFile=getFilePtr(outName,subFolder);
    66. //创建输入文件流
    67. InputStream is = getApplicationContext().getResources().openRawResource(resId);
    68. if(0!=copyData(outFile,is)) {
    69. return null;
    70. }
    71. //返回路径
    72. if(ifReturnParent) {
    73. return outFile.getParent();
    74. } else {
    75. return outFile.getPath();
    76. }
    77. } catch (IOException e) {
    78. e.printStackTrace();
    79. }
    80. return null;
    81. }
    82. public String byteToString(byte[] data) {
    83. int index = data.length;
    84. for (int i = 0; i < data.length; i++) {
    85. if (data[i] == 0) {
    86. index = i;
    87. break;
    88. }
    89. }
    90. byte[] temp = new byte[index];
    91. Arrays.fill(temp, (byte) 0);
    92. System.arraycopy(data, 0, temp, 0, index);
    93. String str;
    94. try {
    95. str = new String(temp, "ISO-8859-1");//ISO-8859-1//GBK
    96. } catch (UnsupportedEncodingException e) {
    97. // TODO Auto-generated catch block
    98. e.printStackTrace();
    99. return "";
    100. }
    101. return str;
    102. }
    在Android FrameWork开发中我们常常要copy文件到特定目录中,本文实现了copy资源到特定目录的功能,
    
  • 相关阅读:
    卷绕工艺与叠片工艺的对比
    C++入门·收尾
    页面查询多项数据组合的线程池设计
    3D孪生场景搭建:模拟仿真
    OpenCV(三十五):凸包检测
    2035. 将数组分成两个数组并最小化数组和的差 折半搜索
    c++——线程安全的单例
    竞赛 深度学习疫情社交安全距离检测算法 - python opencv cnn
    echarts 双堆叠柱状图(数据整理)
    Mockito和Spock实战
  • 原文地址:https://blog.csdn.net/jasonCSH/article/details/126020200