「Update」

1、增加对皮肤包中的Raw文件拷贝到/sdcard/Android/packagename/raw
This commit is contained in:
donghongyu
2024-10-24 10:51:30 +08:00
parent 5c94b004ef
commit 4552201a8c
7 changed files with 118 additions and 10 deletions

View File

@@ -53,6 +53,8 @@ public class SkinAttribute {
for (int i = 0; i < attrs.getAttributeCount(); i++) {
//获得属性名
String attributeName = attrs.getAttributeName(i);
// 筛选配置了换肤属性的View防止因为换肤导致。动态控制了状态的View被还原的问题
//是否符合 需要筛选的属性名
if (mAttributes.contains(attributeName)) {
//属性名写法

View File

@@ -136,7 +136,7 @@ public class SkinManager extends Observable {
/**
* 下载皮肤包
*/
public void selectSkin(Context context, Skin skin) {
public void downloadSkin(Context context, Skin skin) {
File theme = new File(FileUtils.getExternalAppDirectory(context), "theme");
if (theme.exists() && theme.isFile()) {
theme.delete();

View File

@@ -1,9 +1,16 @@
package com.mogo.skin.utils;
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
public class FileUtils {
private static final String TAG = "FileUtils";
/**
* 获取应用特定的外部存储目录。
@@ -34,4 +41,59 @@ public class FileUtils {
// 创建并返回子目录
return new File(externalFilesDir, subDir);
}
/**
* 将指定的Raw文件全都拷贝出来
*
* @param fields 获取raw文件夹的资源ID
*/
public static void copyAppRawToFile(Context context, Field[] fields) {
for (Field field : fields) {// 将资源文件的名称添加到列表中
copyRawToFile(SkinResources.getInstance().getIdentifier(field.getName(), "raw"),
getExternalAppDirectory(context).getAbsolutePath() + "/raw/");
}
}
/**
* 将 raw 目录下的文件复制到外部存储的指定路径
*
* @param rawResId raw 目录下的资源 ID
* @param targetPath 外部存储的目标路径
* @return 是否复制成功
*/
public static boolean copyRawToFile(int rawResId, String targetPath) {
try {
// 获取 raw 目录下的文件输入流
InputStream inputStream = SkinResources.getInstance().getRawInputStream(rawResId);
// 创建目标文件夹
File targetDirectory = new File(targetPath);
if (!targetDirectory.exists()) {
targetDirectory.mkdirs();
}
// 构建目标文件路径
String fileName = SkinResources.getInstance().getResName(rawResId) + ".mp4"; // 根据实际情况更改文件名
File targetFile = new File(targetDirectory, fileName);
// 创建目标文件输出流
OutputStream outputStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputStream.close();
Log.i(TAG, "File copied successfully to " + targetFile.getAbsolutePath());
return true;
} catch (Exception e) {
Log.e(TAG, "Failed to copy file", e);
return false;
}
}
}

View File

@@ -29,8 +29,11 @@ public class SkinResources {
private boolean isDefaultSkin = true;
private Context mContext;
private SkinResources(Context context) {
mContext = context;
mAppResources = context.getResources();
}
@@ -78,6 +81,44 @@ public class SkinResources {
return skinId;
}
/**
* 2.根据名字和类型获取原始app中的ID
*/
public int getIdentifier(String resName, String resType) {
return mAppResources.getIdentifier(resName, resType, mContext.getPackageName());
}
/**
* 1.通过原始app中的resId(R.color.XX)获取到自己的 名字
*/
public String getResName(int resId) {
//在皮肤包中不一定就是 当前程序的 id
//获取对应id 在当前的名称 colorPrimary
//R.drawable.ic_launcher
return mAppResources.getResourceEntryName(resId);//ic_launcher
}
/**
* 将资源 ID 转换为 android.resource:// URL 格式
*
* @param resId 资源 ID
* @return 资源的 android.resource:// URL 地址
*/
public String getResourceUrl(int resId) {
String packageName = mContext.getPackageName();
return "android.resource://" + packageName + "/" + resId;
}
/**
* 1.通过原始app中的resId(R.raw.XX)获取,拷贝到sdk的外部存储的文件路径
*/
public String getRawMp4FilePath(int resId) {
if (isDefaultSkin) {
return getResourceUrl(resId);
}
return "file:///" + FileUtils.getExternalAppDirectory(mContext).getAbsolutePath() + "/raw/" + getResName(resId) + ".mp4";
}
/**
* 获取皮肤中的Raw文件InputStream
*
@@ -97,6 +138,7 @@ public class SkinResources {
/**
* 获取Raw的byte[]
*
* @param resId
* @return
*/
@@ -141,6 +183,7 @@ public class SkinResources {
/**
* 输入主APP的ID到皮肤APK文件中去找到对应ID的颜色值
*
* @param resId
* @return
*/