读取assets中的图片

今天公司有一个游戏需要改造,电脑上找不到游戏的源码,因此,只能反编译来修改,然后重新打包、签名出包。。。

自己先写一个Demo实现要实现的功能,然后打包,再进行反编译将里面的文件拷贝到原游戏反编译后相对应的文件夹中。Demo中有一个加载图片的功能,使用的是直接从drawable中获取,因此图片会在R文件中生成一个int值,可能跟游戏本身中的R文件中的int重复了,导致出错。

为了解决加载图片不要出错,采用了从assets中读取文件的方法。以下是实现的demo:

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
		
	LinearLayout layout = new LinearLayout(this);
	layout.setOrientation(LinearLayout.VERTICAL);
	ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
	layout.setLayoutParams(params);
	ImageView iv = new ImageView(this);
	ViewGroup.LayoutParams iv_params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
	iv.setLayoutParams(iv_params);
	iv.setImageBitmap(getImageFromAssetsFile("a_warn.jpg"));
	layout.addView(iv);
		
	setContentView(layout);
		
	new Handler().postDelayed(new Runnable() {
			
		@Override
		public void run() {
			Intent intent = new Intent(AppActivity.this, PopDiamond.class);
			startActivity(intent);
			AppActivity.this.finish();
		}
	}, 3000);
		
}
	
private Bitmap getImageFromAssetsFile(String fileName)  
	{  
	    Bitmap image = null;  
	    AssetManager am = getResources().getAssets();  
	    try  
	    {  
	        InputStream is = am.open(fileName);  
	        image = BitmapFactory.decodeStream(is);  
	        is.close();  
	    }  
	    catch (IOException e)  
	    {  
	        e.printStackTrace();  
	    }  
	  
	    return image;  
	  
	}
其中从assets读取图片的实现方法为:(可以直接copy调用)

private Bitmap getImageFromAssetsFile(String fileName)  
	  {  
	      Bitmap image = null;  
	      AssetManager am = getResources().getAssets();  
	      try  
	      {  
	          InputStream is = am.open(fileName);  
	          image = BitmapFactory.decodeStream(is);  
	          is.close();  
	      }  
	      catch (IOException e)  
	      {  
	          e.printStackTrace();  
	      }  
	  
	      return image;  
	  
	  }



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值