android 复制asset文件下文件到手机指定路径,手机根目录

“video/video_sample.mp4” 目录解释:asset文件下,有一个文件夹video,video文件夹中,放着一个video_sample.mp4视频文件,文件路径即:video/video_sample.mp4

String targetPath = Constants.getAudioTempPath();

path=FileUtils.copyAssetFile(mContext,"video/video_sample.mp4",targetPath);




public static String copyAssetFile(Context context, String fileName, String targetFilePath) {
        InputStream is = null;
        FileOutputStream os = null;
        try {
            is = context.getAssets().open(fileName);
            if (!new File(targetFilePath).getParentFile().exists()) {
                new File(targetFilePath).getParentFile().mkdirs();
            }
            os = new FileOutputStream(targetFilePath);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
                if (os != null)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return targetFilePath;
    }

手机根目录:String targetPath=”/sdcard/”;

    原文作者:jian11058
    原文地址: https://blog.csdn.net/jian11058/article/details/121262892
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞