在Android中将多个图片转换为字节数组

时间:2014-06-19 14:31:04

标签: android sqlite

我有一个问题,如何将多个图片转换为字节数组(byte []),因为我有个案例在我的数据库sqlite中保存了很多图片,我有一个包含图片的数据列表在drawable文件夹中..

 ArrayList<Integer> imageId = new ArrayList<Integer>(); 
    imageId.add(R.drawable.a1);
    imageId.add(R.drawable.a2);
    imageId.add(R.drawable.a3);
    imageId.add(R.drawable.a4);
    imageId.add(R.drawable.a5);

然后我尝试将此代码转换为字节arrray

    Bitmap b = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.a1);
    //calculate how many bytes our image consists of.
    int bytes = b.getByteCount();
    ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
    b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
    byte[] array = buffer.array();
    System.out.println(array);

下面的代码是有效的,但问题是下面的代码是如何转换一张图片的,现在我需要在arrayList中转换多张图片,有人可以帮帮我吗?因为我试过循环,它给我一个错误,java.lang.OutOfMemoryError ..

1 个答案:

答案 0 :(得分:0)

确定,

首先不要将整张照片保存到SqLite。这不是一个合适的方式。这是一个非常简单的解决方案,但风险很小......

只需将图片保存到SD Card WhatsApp,然后将其路径保存到SqLite。因此,无论何时您想要访问图片,您都可以从Sqlite读取它们的路径并访问它们。

但风险是图片处于用户控制之下,因此他们可以将其删除。但你可以深入隐藏图片:)