1. 图片放在sdcard中,

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

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

res文件夹下获取成Bitmap主要可利用BitmapFacory.decode*()方法;

如果要获取成Drawable则直接getResource().getDrawable(int id);

上面的都是利用资源的int型资源id获取,下面讲解一下利用图像资源的文件名获取资源的方法。

  //得到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目录,这里可以存放只读文件

资源获取的方式为

InputStream is = getResources().getAssets().open(name);

更多相关文章

  1. [android]MonkeyRunner-andrec实现思路
  2. android实现为PreferenceScreen设置背景图片等
  3. Android(安卓)Support Annotation的使用
  4. Android中获取定位经纬度信息
  5. Android使用图片资源
  6. Android(安卓)仿ios下拉放大图片过度拉伸效果的最佳方案
  7. 【Android(安卓)frameLayout布局】使用framelayout布局将图片和
  8. android是如何加载资源图片的
  9. android图片上传服务器

随机推荐

  1. Android退出程序的多种方法
  2. Android:获取APK签名信息 .
  3. 安卓巴士Android开发神贴整理
  4. Android中自定义对话框小结
  5. android appwidget service的初始化
  6. Android查询短信数据库
  7. 几个通用的类,迷你型的Android下载框架
  8. Android(安卓)修改默认输入法
  9. 自定义自己的AlertDialog
  10. Android(安卓)的整体布局