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

 1. 图片放在sdcard中,
  Bitmap imageBitmap = BitmapFactory.decodeFile(path) (path 是图片的路径,跟目录是/sdcard)

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

  //得到application对象
  ApplicationInfo appInfo = getApplicationInfo();
  //得到该图片的id(name 是该图片的名字,drawable 是该图片存放的目录,appInfo.packageName是应用程序的包)
  int resID = getResources().getIdentifier(name, drawable, appInfo.packageName);

  //代码如下
  public Bitmap getRes(String name)
    {
      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);    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值