男人应似海  

                                                         文字和图片的资源管理类ResManager

功能

加载工程目录中assets文件下的文件

代码

/**
 * 类名:ResManager.java
 * @author wader
 * 类描述:获取工程中assets目下的文字、图片等资源
 * 创建时间:2011-11-29 16:07
 */
public class ResManager {
 /**
  * 图片格式
  */
 private static final String IMAGE_FILE_FORMAT = ".png";
 /**
  * 文本文件格式
  */
 private static final String TEXT_FILE_FORMAT = ".properties";

 /**
  * 图片存放的路径
  */
 public final static String IMAGES_DIR = "images/";

 // public final static String IMAGES_DIR_480 = "images_480/";
 public final static String TEXTS_DIR = "textRes/";
 /**
  * 文件路径
  */
 private static String filePath = "";

 /**
  * 从工程资源加载图片资源(路径是assets/images/**.png)
  *
  * @param fileName
  *            图片资源路径
  */
 public static Bitmap loadImageRes(Activity activity, int screenWidth,
   String fileName) {
  Bitmap bitmap = null;
  InputStream is = null;
  FileInputStream fis = null;
  filePath = IMAGES_DIR;
  // 这里可以根据分辨率等进行路径区分判断
  // if (screenWidth >= 480) {
  // filePath = IMAGES_DIR_480;
  // }
  try {
   is = activity.getAssets().open(
     filePath + fileName + IMAGE_FILE_FORMAT);
   if (is != null) {
    bitmap = BitmapFactory.decodeStream(is);
   }
  } catch (Exception e) {
  } finally {
   try {
    if (is != null) {
     is.close();
    }
    if (fis != null) {
     fis.close();
    }
   } catch (Exception e) {
   } finally {
    is = null;
    fis = null;
   }
  }
  return bitmap;
 }

 /**
  * 从工程资源加载文字资源(路径是:assets/textRes/**.properties)
  *
  * @param fileName
  */
 public static ArrayList<String> loadTextRes(String fileName, Context context) {
  filePath = TEXTS_DIR;
  return loadProperties(filePath + fileName + TEXT_FILE_FORMAT, context);
 }

 /**
  * 读取配置文件读取配置信息
  *
  * @param filename
  *            配置文件路径
  * @return 包含配置信息的hashmap键值对
  */
 private static ArrayList<String> loadProperties(String filename,
   Context context) {
  Log.d("loadProperties", "loadProperties");
  ArrayList<String> properties = new ArrayList<String>();
  InputStream is = null;
  FileInputStream fis = null;
  InputStreamReader rin = null;

  // 将配置文件放到res/raw/目录下,可以通过以下的方法获取
  // is = context.getResources().openRawResource(R.raw.system);

  // 这是读取配置文件的第二种方法
  // 将配置文件放到assets目录下,可以通过以下的方法获取
  // is = context.getAssets().open("system.properties");

  // 用来提取键值对的临时字符串
  StringBuffer tempStr = new StringBuffer();

  // 用来存放读取的每个字符
  int ch = 0;

  // 用来保存读取的配置文件一行的信息
  String line = null;
  try {
   Log.d("loadProperties", "the filename is: " + filename);
   is = context.getAssets().open(filename);
   rin = new InputStreamReader(is, "UTF-8");

   while (ch != -1) {
    tempStr.delete(0, tempStr.length());
    while ((ch = rin.read()) != -1) {
     if (ch != '\n') {
      tempStr.append((char) ch);
     } else {
      break;
     }
    }
    line = tempStr.toString().trim();
    Log.d("loadProperties", "line: " + line);
    // 判断读出的那行数据是否有效,#开头的代表注释,如果是注释行那么跳过下面,继续上面操作
    if (line.length() == 0 || line.startsWith("#")) {
     continue;
    }
    properties.add(line);
   }
  } catch (IOException e) {
   // LogX.trace("read property file", e.getMessage() + "fail");
  } finally {
   try {
    if (is != null) {
     is.close();
    }
    if (fis != null) {
     fis.close();
    }
    if (null != rin) {
     rin.close();
    }
   } catch (IOException e) {
    // LogX.trace("read property file", e.getMessage() + "fail");
   } finally {
    is = null;
    fis = null;
    rin = null;
   }
  }
  return properties;
 }
}

posted on 2011-11-30 13:14  男人应似海  阅读(1697)  评论(0编辑  收藏  举报