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

​
private File getFilePtr(String outName,String subFolder) throws IOException {
    //找到目录
    File filesDir = getApplicationContext().getFilesDir();
    if (!filesDir.exists()) {
        filesDir.mkdirs();
    }
    //创建专属目录
    File outFileFolder = new File(filesDir.getAbsolutePath()+"/target/"+subFolder);
    if(!outFileFolder.exists()) {
        outFileFolder.mkdirs();
    }
    //创建输出文件
    File outFile=new File(outFileFolder,outName);
    String outFilename = outFile.getAbsolutePath();
    Log.i(TAG, "outFile is " + outFilename);
    if (!outFile.exists()) {
        boolean res = outFile.createNewFile();
        if (!res) {
            Log.e(TAG, "outFile not exist!(" + outFilename + ")");
            return null;
        }
    }
    return outFile;
}
private int copyData(File outFile,InputStream is){
    try {
        FileOutputStream fos = new FileOutputStream(outFile);
        //分段读取文件,并写出到输出文件,完成拷贝操作。
        byte[] buffer = new byte[1024];
        int byteCount;
        while ((byteCount = is.read(buffer)) != -1) {
            fos.write(buffer, 0, byteCount);
        }
        fos.flush();
        is.close();
        fos.close();
        return 0;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}

public String getFilePathAfterCopy(Uri uri,String outName,String subFolder,boolean ifReturnParent){
    try {
        File outFile=getFilePtr(outName,subFolder);
        //创建输入文件流
        InputStream is= this.getContentResolver().openInputStream(uri);
        if(0!=copyData(outFile,is)) {
            return null;
        }
        //返回路径
        if(ifReturnParent) {
            return  outFile.getParent();
        } else {
            return outFile.getPath();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
public String getFilePathAfterCopy(int resId,String outName,String subFolder,boolean ifReturnParent) {
    try {
        //找到目录
        File outFile=getFilePtr(outName,subFolder);
        //创建输入文件流
        InputStream is = getApplicationContext().getResources().openRawResource(resId);
        if(0!=copyData(outFile,is)) {
            return null;
        }
        //返回路径
        if(ifReturnParent) {
            return  outFile.getParent();
        } else {
            return outFile.getPath();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
public String byteToString(byte[] data) {
    int index = data.length;
    for (int i = 0; i < data.length; i++) {
        if (data[i] == 0) {
            index = i;
            break;
        }
    }
    byte[] temp = new byte[index];
    Arrays.fill(temp, (byte) 0);
    System.arraycopy(data, 0, temp, 0, index);
    String str;
    try {
        str = new String(temp, "ISO-8859-1");//ISO-8859-1//GBK
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    }
    return str;
}

​
在Android FrameWork开发中我们常常要copy文件到特定目录中,本文实现了copy资源到特定目录的功能,
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值