1. 获取包info文件信息
Context context = AppStartActitvity.this;
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),0);
String version = packageInfo.versionName;
代码封封装示例:
public static String getVersionNum(Context context) {
String version = "";
try {
version = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0).versionName;
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} finally {
}
return version;
}
2. 获取本地资源文件
图片获取:InputStream is = this.getResources()
.openRawResource(R.drawable.loading);
代码示例:
public Bitmap readBit() {
InputStream is = this.getResources()
.openRawResource(R.drawable.loading);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
// options.inSampleSize = 10; // width,hight设为原来的十分一
Bitmap btp = BitmapFactory.decodeStream(is, null, options);
return btp;
}
3. 获取网络资源
关于InputStream.read(byte[] b)和InputStream.read(byte[] b,int off,int len)这两个方法都是用来从流里读取多个字节的,有经验的程序员就会发现,这两个方法经常 读取不到自己想要读取的个数的字节。比如第一个方法,程序员往往希望程序能读取到b.length个字节,而实际情况是,系统往往读取不了这么多。仔细阅读Java的API说明就发现了,这个方法 并不保证能读取这么多个字节,它只能保证最多读取这么多个字节(最少1个)。因此,如果要让程序读取count个字节,最好用以下代码:
byte[] b = new byte[count];
int readCount = 0; // 已经成功读取的字节的个数
while (readCount < count) {
readCount += in.read(bytes, readCount, count - readCount);
}
用这段代码可以保证读取count个字节,除非中途遇到IO异常或者到了数据流的结尾(EOFException)