From 514fc054a7b097672037b1fea880959ffebb9355 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 22 May 2025 18:35:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 2 + .../java/com/xypower/common/FilesUtils.java | 81 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3f83a0aa..2d86227e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,6 +14,8 @@ + diff --git a/common/src/main/java/com/xypower/common/FilesUtils.java b/common/src/main/java/com/xypower/common/FilesUtils.java index cca56fa6..1b890b09 100644 --- a/common/src/main/java/com/xypower/common/FilesUtils.java +++ b/common/src/main/java/com/xypower/common/FilesUtils.java @@ -1,5 +1,7 @@ package com.xypower.common; +import android.content.Context; +import android.content.res.AssetManager; import android.text.TextUtils; import org.w3c.dom.Text; @@ -320,4 +322,83 @@ public class FilesUtils { } } + public static void copyAssetsDir(Context context, String directory, String destPath) { + try { + AssetManager assetManager = context.getAssets(); + String[] fileList = assetManager.list(directory); + if (fileList != null && fileList.length > 0) { + File file = new File(destPath); + if (!file.exists()) { + file.mkdirs(); + } + + if (!directory.endsWith(File.separator)) { + directory += File.separator; + } + if (!destPath.endsWith(File.separator)) { + destPath += File.separator; + } + + for (String fileName : fileList) { + copyAssetsDir(context, directory + fileName, destPath + fileName); + } + } else { + // Try to file + copyAssetsFile(context, directory, destPath); + } + } catch (Exception e) { + e.printStackTrace(); + } + + // else {//如果是文件 + // InputStream inputStream=context.getAssets().open(filePath); + // File file=new File(context.getFilesDir().getAbsolutePath()+ File.separator+filePath); + // Log.i("copyAssets2Phone","file:"+file); + // if(!file.exists() || file.length()==0) { + // FileOutputStream fos=new FileOutputStream(file); + // int len=-1; + // byte[] buffer=new byte[1024]; + // while ((len=inputStream.read(buffer))!=-1){ + // fos.write(buffer,0,len); + // } + // fos.flush(); + // inputStream.close(); + // fos.close(); + // showToast(context,"模型文件复制完毕"); + // } else { + // showToast(context,"模型文件已存在,无需复制"); + // } + // } + } + + + public static void copyAssetsFile(Context context, String fileName, String destPath) { + InputStream inputStream = null; + FileOutputStream fos = null; + try { + inputStream = context.getAssets().open(fileName); + //getFilesDir() 获得当前APP的安装路径 /data/data/包名/files 目录 + File file = new File(destPath); + if (file.exists()) { + file.delete(); + } + + fos = new FileOutputStream(file); + int len = -1; + byte[] buffer = new byte[1024]; + while ((len = inputStream.read(buffer)) != -1) { + try { + fos.write(buffer, 0, len); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + fos.flush(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + FilesUtils.closeFriendly(inputStream); + FilesUtils.closeFriendly(fos); + } + } }