实例介绍
【实例截图】
【核心代码】
package ms.TreeView;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
//javaapk.com提供测试
/**
* 类名: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;
}
}
标签: listview
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论