文件头header描述逐帧图的详细信息,如帧数行数列数和权限等级等等,这些信息规则由自己定义。
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ResLoader{ /**
*
* @param header 自定义指定规则的文件头信息
* @param fileName 图片文件源
* @param destDir 文件转换后的存储位置
*/
public static void convertMyRes(String header,String fileName,String destDir) {
try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = fis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
byte[] data = bos.toByteArray();
DataOutputStream dos = new DataOutputStream(new FileOutputStream(
destDir + "/" + fileName.substring(0, fileName.lastIndexOf("."))+".d"));
dos.writeBytes(header);
dos.write(data, 0, data.length);
fis.close();
bos.close();
dos.close();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
*
* @param path 文件读取路径
* @param headBytes 文件头的字节数
* @return 返回HashMap<String,Bitmap>文件头为键值,图片作为内容
*/
@SuppressWarnings("deprecation")
public static HashMap<String,Bitmap> readMyRes(String path,int headBytes) {
try {
File file = new File(path);
//File[] files=fileDir.listFiles();
//String[] head
String header = null;
DataInputStream dis = new DataInputStream(new FileInputStream(file));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
dis.read(buffer, 0, headBytes);
header=new String(buffer, headBytes);
while ((len = dis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
byte[] data = bos.toByteArray();
Bitmap bmp=BitmapFactory.decodeByteArray(data, 0, data.length);
HashMap<String, Bitmap> map=new HashMap<String,Bitmap>();
map.put(header,bmp);
return map;
} catch (Exception e) {
return null;
}
}
}
|