【发布时间】:2015-07-22 10:33:52
【问题描述】:
有没有办法知道一个字节数组是否是一个有效的图像?
我正在拍照,在 OnActivityResult 中:
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 75, out);
//arrayFotos is an ArrayList
arrayFotos.add(new BeanFotos("", imageBitmap, lastKnwonLocation.getLatitude(), lastKnwonLocation.getLongitude()));
我将它存储在数据库中:
//listaFotos and arrayFotos are the same array, but with different names, because I am storing it in another class in my app
for(int i=0;i<listaFotos.size();i++){
values.clear();
values.put("codficha", codficha);
int bytes=listaFotos.get(i).getFoto().getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
listaFotos.get(i).getFoto().copyPixelsToBuffer(buffer);
values.put("foto", buffer.array());
db.insert("fotos", null, values);
}
稍后,我需要显示该图像:
byte[] arrayFoto = csr2.getBlob(0);
Bitmap fotoBitmap=BitmapFactory.decodeByteArray(arrayFoto, 0, arrayFoto.length);
问题是,无论我做什么,decodeByteArray 方法总是返回 null。我尝试过使用外部库,它总是为空,所以我想也许我以错误的方式存储它。 arrayFoto 变量不为空,我正在接收字节(准确地说是 84584),但 decodeByteArray 方法返回空。
那么,有没有办法让我确定字节数组是正确的图像?
谢谢。
【问题讨论】:
标签: android bitmap bytearray android-image