读取assets中的图片

来源:互联网 发布:mac的library在哪 编辑:程序博客网 时间:2024/05/20 11:20

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

自己先写一个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() {@Overridepublic 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;      }



0 0