首页 > 学院 > 开发设计 > 正文

读取assets中的图片

2019-11-07 23:56:43
字体:
来源:转载
供稿:网友

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

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

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

@OverridePRotected 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;  	  	  }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表