import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.util.EncodingUtils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ResourceUtils {
publicstatic final String ENCODING = "UTF-8";
//从resources中的raw 文件夹中获取文件并读取数据
publicstatic String getFromRaw(Context context, int rawResourceId){
String result = "";
try {
InputStream in =context.getResources().openRawResource(
rawResourceId);
// 获取文件的字节数
int lenght = in.available();
// 创建byte数组
byte[] buffer = new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer,ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 从assets文件夹中获取文件并读取数据
publicstatic String getFromAssets(Context context, String fileName){
String result = "";
try {
InputStream in =context.getResources().getAssets().open(fileName);
// 获取文件的字节数
int lenght = in.available();
// 创建byte数组
byte[] buffer = new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer,ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
publicstatic Bitmap getImageFromAssets(Context context, String fileName){
Bitmap bitmap;
try {
InputStream is =context.getResources().getAssets().open(fileName);
bitmap = BitmapFactory.decodeStream(newFlushedInputStream(is));
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
static classFlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStreaminputStream) {
super(inputStream);
}
@Override
public long skip(long n) throws IOException{
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n){
long bytesSkipped = in.skip(n -totalBytesSkipped);
if (bytesSkipped == 0L) {
int b = read();
if (b < 0) {
break; // we reached EOF
} else {
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
}
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.util.EncodingUtils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ResourceUtils {
}