我正在尝试将位图列表保存到文件中。基本上,我想存储它以供将来参考,这样当用户从图库中选择图像时,即使应用程序关闭,它也会保留在那里。
这就是我想要做的?但是得到了一些异常——BufferUnderFlowException、OutOfMemoryException。问题似乎是 Bitmap List 的内存非常大。有没有更好的方法来做到这一点?
final List<Bitmap> bitmapcontent=new ArrayList<Bitmap>();
ImageView imageView;
Gallery gallery;
private static byte[] bytesar;
ByteBuffer dst;
private void savethebitmap() throws IOException, ClassNotFoundException {
try {
FileOutputStream fos = openFileOutput("bitmapimage", Context.MODE_PRIVATE);
ObjectOutputStream out=new ObjectOutputStream(fos);
out.writeInt(bitmapcontent.size());
for(int i=0;i<bitmapcontent.size();i++){
out.writeInt(bitmapcontent.get(i).getRowBytes());
out.writeInt(bitmapcontent.get(i).getHeight());
out.writeInt(bitmapcontent.get(i).getWidth());
int bmSize = bitmapcontent.get(i).getRowBytes() * bitmapcontent.get(i).getHeight();
if(dst==null || bmSize > dst.capacity())
dst= ByteBuffer.allocate(bmSize);
out.writeInt(dst.capacity());
dst.position(0);
bitmapcontent.get(i).copyPixelsToBuffer(dst);
if(bytesar==null || bmSize > bytesar.length)
bytesar=new byte[bmSize];
dst.position(0);
dst.get(bytesar);
out.write(bytesar, 0, bytesar.length);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void loadthebitmap() throws IOException,ClassNotFoundException{
try {
freeBitmaps();
FileInputStream fos = openFileInput("bitmapimage");
ObjectInputStream in=new ObjectInputStream(fos);
int size=in.readInt();
for(int i=0;i<size;i++){
int height=in.readInt();
int width=in.readInt();
int bmSize=in.readInt();
if(bytesar==null || bmSize > bytesar.length)
bytesar= new byte[bmSize];
int offset=0;
while(in.available()>0){
offset=offset + in.read(bytesar, offset, in.available());
}
if(dst==null || bmSize > dst.capacity())
dst= ByteBuffer.allocate(bmSize);
dst.position(0);
dst.put(bytesar);
dst.position(0);
Bitmap myVideoScreenshotBm=Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
myVideoScreenshotBm.copyPixelsFromBuffer(dst);
bitmapcontent.add(myVideoScreenshotBm);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}