Android APK加载外部资源文件

本文介绍了如何在Android应用中加载外部资源文件,包括利用AssetManager和Resource进行资源加载,详细讲解了创建AssetManager和Resource的具体步骤,并给出了简单的测试案例,涉及主app与资源包app的设置与资源加载。
摘要由CSDN通过智能技术生成

1、背景

看了点东西,写点东西

2、资源加载

总所周知,Android资源加载通过Resource和AssetManager,比如一个textview的setText过程(api 21):

1、textView.setText(R.string.str);
//调用Resrouce的getText()
2public final void setText(int resid) {
   
        setText(getContext().getResources().getText(resid));
    }
//调用AssetManager的getResourceText()方法
3public CharSequence getText(int id) throws NotFoundException {
   
        CharSequence res = mAssets.getResourceText(id);
        if (res != null) {
   
            return res;
        }
        throw new NotFoundException("String resource ID #0x"
                                    + Integer.toHexString(id));
    }
//AssetManager的getResourceText()具体实现
4final CharSequence getResourceText(int ident) {
   
        synchronized (this) {
   
            TypedValue tmpValue = mValue;
            int block = loadResourceValue(ident, (short) 0, tmpValue, true);
            if (block >= 0) {
   
                if (tmpValue.type == TypedValue.TYPE_STRING) {
   
                    return mStringBlocks[block].get(tmpValue.data);
                }
                return tmpValue.coerceToString();
            }
        }
        return null;
    }

而AssetManager提供了一个方法addAssetPath()来加载外部资源

	/**
     * Add an additional set of assets to the asset manager.  This can be
     * either a directory or ZIP file.  Not for use by applications.  Returns
     * the cookie of the added asset, or 0 on failure.
     * {@hide}
     */
    public final int addAssetPath(String path) {
   
        synchronized (this) {
   
            int res = addAssetPathNative(path);
            makeStringBlocks(mSt
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值