package com.lonelypluto.loadresourcedemo.utils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.Log;
import com.lonelypluto.loadresourcedemo.bean.LoadResourceBean;
import com.lonelypluto.loadresourcedemo.constants.SPConsts;
import java.io.File;
import java.lang.reflect.Method;
import dalvik.system.DexClassLoader;
/**
* @Description: 资源加载工具类
* @author: ZhangYW
* @time: 2018/9/17 11:16
*/
public class LoadResourceUtil {
private static final String TAG = LoadResourceUtil.class.getSimpleName();
private static final String RESOURCE_TYPE_DRAWABLE = "drawable";// 图片
private static final String RESOURCE_TYPE_STRING = "string";// 文字
private static final String RESOURCE_TYPE_COLOR = "color";// 颜色
private Context mContext;
private String mDexDir;// 资源路径
private static LoadResourceBean mResourceLoadBean;// 资源对象
/**
* 静态内部类
*/
private static class LoadResourceUtilHolder {
private static final LoadResourceUtil INSTANCE = new LoadResourceUtil();
}
/**
* 单例模式
* @return
*/
public static final LoadResourceUtil getInstance() {
return LoadResourceUtilHolder.INSTANCE;
}
/**
* 初始化
*
* @param context
* @param resourcePath 本地资源路径
*/
public void init(Context context, String resourcePath) {
Log.e(TAG, "init");
mContext = context.getApplicationContext();
File dexDir = mContext.getDir("dex", Context.MODE_PRIVATE);
if (!dexDir.exists()) {
dexDir.mkdir();
}
mDexDir = dexDir.getAbsolutePath();
mResourceLoadBean = getResourceLoad(resourcePath);
}
/**
* 加载未安装资源包
*
* @param resourcePath 未安装资源包
* @return
*/
public LoadResourceBean getResourceLoad(String resourcePath) {
Log.e(TAG, "getLoadResource");
LoadResourceBean resourceLoadBean = null;
// 获取未安装APK的PackageInfo
PackageInfo info = queryPackageInfo(resourcePath);
if (info != null) {
// 获取成功
try {
// 创建AssetManager实例
AssetManager assetManager = AssetManager.class.newInstance();
Class cls = AssetManager.class;
Method method = cls.getMethod("addAssetPath", String.class);
// 反射设置资源加载路径
method.invoke(assetManager, resourcePath);
// 构造出正确的Resource
Resources resources = new Resources(assetManager, mContext.getResources().getDisplayMetrics(),
mContext.getResources().getConfiguration());
resourceLoadBean = new LoadResourceBean();
resourceLoadBean.setResources(resources);
resourceLoadBean.setPackageName(info.packageName);
// 设置正确的类加载器, 因为需要去加载R文件
resourceLoadBean.setClassLoader(new DexClassLoader(resourcePath, mDexDir, null,
mContext.getClassLoader()));
} catch (Exception e) {
e.printStackTrace();
}
}
return resourceLoadBean;
}
/**
* 重新加载未安装应用资源包
*
* @param resourcePath 未安装资源包路径
* @return
*/
public void setLoadResource(String resourcePath) {
// 保存资源包路径 下次进入程序默认加载
SharedPreferencesUtil.put(SPConsts.SP_RESOURCE_PATH, resourcePath);
mResourceLoadBean = getResourceLoad(resourcePath);
}
/**
* 获取未安装资源包信息
*
* @param resourcePath 未安装资源包路径
* @return
*/
private PackageInfo queryPackageInfo(String resourcePath) {
return mContext.getPackageManager().getPackageArchiveInfo(resourcePath, PackageManager.GET_ACTIVITIES);
}
/**
* 获取未安装资源Drawable
*
* @param fieldName 资源名
* @return
*/
public Drawable getDrawable(String fieldName) {
Drawable drawable = null;
int resourceID = getResourceID(mResourceLoadBean.getPackageName(), RESOURCE_TYPE_DRAWABLE, fieldName);
if (mResourceLoadBean != null) {
drawable = mResourceLoadBean.getResources().getDrawable(resourceID);
}
return drawable;
}
/**
* 获取未安装资源String
*
* @param fieldName 资源名
* @return
*/
public String getString(String fieldName) {
String string = null;
int resourceID = getResourceID(mResourceLoadBean.getPackageName(), RESOURCE_TYPE_STRING, fieldName);
if (mResourceLoadBean != null) {
string = mResourceLoadBean.getResources().getString(resourceID);
}
return string;
}
/**
* 获取未安装资源color
*
* @param fieldName 资源名
* @return
*/
public int getColor(String fieldName) {
int color = 0;
int resourceID = getResourceID(mResourceLoadBean.getPackageName(), RESOURCE_TYPE_COLOR, fieldName);
if (mResourceLoadBean != null) {
color = mResourceLoadBean.getResources().getColor(resourceID);
}
return color;
}
/**
* 获取未安装资源的ID
*
* @param packageName 包名
* @param type 资源类型
* @param fieldName 资源名
* @return
*/
public int getResourceID(String packageName, String type, String fieldName) {
int resID = 0;
String rClassName = packageName + ".R$" + type;
try {
Class cls = mResourceLoadBean.getClassLoader().loadClass(rClassName);
resID = (Integer) cls.getField(fieldName).get(null);
} catch (Exception e) {
e.printStackTrace();
}
return resID;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android动态加载外部资源文件
共311个文件
xml:171个
png:33个
bin:27个
需积分: 50 10 下载量 80 浏览量
2019-02-27
10:31:33
上传
评论 1
收藏 3.69MB RAR 举报
温馨提示
读取本地不同的资源文件apk,获取不同的图片、文字和颜色,详情请看https://blog.csdn.net/zyw0101/article/details/87939253
资源推荐
资源详情
资源评论
收起资源包目录
android动态加载外部资源文件 (311个子文件)
testResource1.apk 1.46MB
testResource2.apk 1.45MB
gradlew.bat 2KB
gradlew.bat 2KB
gradlew.bat 2KB
taskHistory.bin 1.19MB
taskHistory.bin 758KB
taskHistory.bin 748KB
classAnalysis.bin 580KB
classAnalysis.bin 554KB
classAnalysis.bin 549KB
jarAnalysis.bin 249KB
fileHashes.bin 238KB
jarAnalysis.bin 238KB
jarAnalysis.bin 238KB
fileHashes.bin 203KB
taskHistory.bin 198KB
fileHashes.bin 189KB
taskHistory.bin 128KB
taskHistory.bin 73KB
taskJars.bin 23KB
taskJars.bin 23KB
taskJars.bin 23KB
outputFiles.bin 21KB
resourceHashesCache.bin 20KB
outputFiles.bin 20KB
outputFiles.bin 20KB
resourceHashesCache.bin 20KB
resourceHashesCache.bin 20KB
last-build.bin 1B
last-build.bin 1B
last-build.bin 1B
.gitignore 187B
.gitignore 187B
.gitignore 187B
.gitignore 8B
.gitignore 8B
.gitignore 8B
build.gradle 965B
build.gradle 888B
build.gradle 888B
build.gradle 573B
build.gradle 573B
build.gradle 573B
settings.gradle 16B
settings.gradle 16B
settings.gradle 16B
gradlew 5KB
gradlew 5KB
gradlew 5KB
app.iml 15KB
app.iml 14KB
app.iml 14KB
TestResourceApk2.iml 893B
TestResourceApk1.iml 893B
LoadResourceDemo.iml 893B
gradle-wrapper.jar 53KB
gradle-wrapper.jar 53KB
gradle-wrapper.jar 53KB
LoadResourceUtil.java 6KB
MainActivity.java 6KB
SharedPreferencesUtil.java 2KB
LoadResourceBean.java 975B
ExampleInstrumentedTest.java 774B
ExampleInstrumentedTest.java 774B
ExampleInstrumentedTest.java 774B
MyApplication.java 575B
ExampleUnitTest.java 409B
ExampleUnitTest.java 409B
ExampleUnitTest.java 409B
SPConsts.java 400B
javaCompile.lock 17B
fileContent.lock 17B
fileHashes.lock 17B
taskHistory.lock 17B
buildOutputCleanup.lock 17B
javaCompile.lock 17B
fileContent.lock 17B
fileHashes.lock 17B
taskHistory.lock 17B
buildOutputCleanup.lock 17B
javaCompile.lock 17B
fileContent.lock 17B
fileHashes.lock 17B
taskHistory.lock 17B
buildOutputCleanup.lock 17B
ic_launcher_round.png 15KB
ic_launcher_round.png 15KB
ic_launcher_round.png 15KB
test_img.png 11KB
ic_launcher_round.png 10KB
ic_launcher_round.png 10KB
ic_launcher_round.png 10KB
ic_launcher.png 9KB
ic_launcher.png 9KB
ic_launcher.png 9KB
ic_launcher_round.png 7KB
ic_launcher_round.png 7KB
ic_launcher_round.png 7KB
ic_launcher.png 6KB
共 311 条
- 1
- 2
- 3
- 4
资源评论
孤独的冥王星
- 粉丝: 1176
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功