【问题标题】:How to save an Bitmap ArrayList to a file如何将位图 ArrayList 保存到文件中
【发布时间】:2012-12-04 04:48:44
【问题描述】:

我正在尝试将位图列表保存到文件中。基本上,我想存储它以供将来参考,这样当用户从图库中选择图像时,即使应用关闭,它也会保留在那里。

这就是我想要做的?但是得到了一些异常——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();
    }

【问题讨论】:

  • 同时发布 printStackTrace。

标签: java android image list bitmap


【解决方案1】:

out.writeObject(bitmapContent) 和 Bob(应该是)你的叔叔......如果你的内存不足,请使用建议来增加你的内存大小。还有further tips。祝你好运,让我知道你过得怎么样。

【讨论】:

    【解决方案2】:

    ObjectOutputStream 缓存写入它的对象。所以要么使用简单的DataOutputStream,要么调用ObjectOutputStream.reset

        out.write(bytesar, 0, bytesar.length);
        out.flush();
        out.reset();
    

    【讨论】:

    • 它有帮助,但 OutOfMemoryException 仍然没有消失。
    【解决方案3】:

    可能是arraylist太大,堆无法处理。尝试使用以下命令启动 VM:

    -Xmx2G -Xms1G
    

    -Xmx 代表最大内存,-Xms 代表在开始时分配的内存。 G 代表 GB,M 代表 MB。

    在上面的评论中回复G_S,他好像用ObjectOutputStream序列化了arraylist。

    编辑:

    如果设备没有太多内存,您可能想尝试将位图分解成块,例如块。

    【讨论】:

    • 安卓设备不知道哪来这么多内存。
    猜你喜欢
    • 1970-01-01
    • 2013-11-04
    • 2014-05-11
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 2015-04-30
    • 2016-10-28
    相关资源
    最近更新 更多