Android获取图片资源的4种方式

1. 图片放在sdcard中,android

  Bitmap p_w_picpathBitmap = BitmapFactory.decodeFile(path) (path 是图片的路径,跟目录是/sdcard)app


2. 图片在项目的res文件夹下面ide


  //获得application对象this

  ApplicationInfo appInfo = getApplicationInfo();code

  //获得该图片的id(name 是该图片的名字,drawable 是该图片存放的目录,appInfo.packageName是应用程序的包)xml

  int resID = getResources().getIdentifier(name, "drawable", "appInfo.packageName");对象


  //代码以下图片

  public Bitmap getRes(String name)get

{it

   ApplicationInfo appInfo = getApplicationInfo();

   int resID = getResources().getIdentifier(name, "drawable", "appInfo.packageName");

   return BitmapFactory.decodeResource(getResources(), resID);

  }


3. 图片放在src目录下

  String path = com/xiangmu/test.png; //图片存放的路径

  InputStream is = getClassLoader().getResourceAsStream(path); //获得图片流


4.android中有个Assets目录,这里能够存放只读文件

方法1:

AssetManager assetManager = this.getResources().getAsset();

InputStream fis = assetManager.open("db.properties");

简写为:

InputStream fis = getResources().getAssets().open("db.properties");

方法2:

InputStream abpath = getClass().getResourceAsStream("/assets/文件名");




5.访问res/raw目录中的文件(//res/raw目录下存放,好比test.xml一个二进制文件,咱们能够读取能够直接)

InputStream is=context.getResources().openRawResource(R.raw.test);

相关文章
相关标签/搜索