我有一个问题,如何将多张图片转换成字节数组(byte []),因为我有情况在我的数据库sqlite中保存许多图片,我在可绘制的文件夹中有包含图片的数组列表。
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);然后我试着把这段代码转换成字节数组
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..
发布于 2014-06-19 22:57:50
好的,
首先,不要将整个图片保存到SqLite。这不是一种合适的方式。有一个非常简单的解决方案,但有一点风险。
只需像WhatsApp一样将图像保存到SD Card,并将其路径保存到SqLite即可。所以,当你想要访问你的图片时,你可以从Sqlite读取它们的路径并访问它们。
但风险是图片在用户控制之下,所以他们可以删除它。但是你可以用一种很深的方式隐藏这些图片:)
https://stackoverflow.com/questions/24309127
复制相似问题