Serializer et deserializer une Bitmap
Bonjour, j'ai un problème pour sérialiser et désérialiser une Bitmap. J'ai essayer plusieurs choses :
Serializer un tableau de bytes, à chaque que je le déserialize, je me retrouve avec un tableau remplit de 0 : image noire.
J'ai ensuite essayé de convertir ce tableau en string, puis de le serializer et le deserializer, pareil. Si quelqu'un à une idée, voilà le code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
public static void serializerBitmap(String path,String urlKey, Bitmap bmp) throws IOException{
ByteBuffer buffer = null;
String url = "";
if(urlKey.contains("http://")){
url = urlKey.substring(6, urlKey.length()-4)+".dat";
}
else{
url = urlKey.substring(0, urlKey.length()-4)+".dat";
}
String urlFinal ="";
for(String s : url.split("/")){
urlFinal = urlFinal + s;
}
String completePath = path+urlFinal;
FileOutputStream fichier = new FileOutputStream(completePath);
ObjectOutputStream out = new ObjectOutputStream(fichier);
out.writeInt(bmp.getHeight());
out.writeInt(bmp.getWidth());
int bmSize = bmp.getRowBytes() * bmp.getHeight();
buffer = ByteBuffer.allocate(bmSize);
buffer.position(0);
bmp.copyPixelsToBuffer(buffer);
byte[] bytes = buffer.array();
String strBytes = new String(bytes,"UTF-8");
buffer.position(0);
out.writeObject(urlKey);
out.writeInt(bmSize);
out.writeObject(strBytes);
imagesEnCache.put(urlKey, bmp);
out.close();
out.flush();
}
public static void deserializerBitmap(String path) throws IOException, ClassNotFoundException{
File parent = new File(path);
for(File f : parent.listFiles()){
if(f.getAbsolutePath().contains("images")){
FileInputStream fichier = new FileInputStream(f);
ObjectInputStream in = new ObjectInputStream(fichier);
int height=in.readInt();
int width=in.readInt();
String key = (String)in.readObject();
int size = in.readInt();
byte[] bytes = new byte[size];
String strBytes = (String)in.readObject();
ByteBuffer buffer = null;
buffer = ByteBuffer.allocate(size);
bytes = strBytes.getBytes("UTF-8");
buffer.position(0);
buffer.put(bytes);
buffer.position(0);
in.close();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
bmp.copyPixelsFromBuffer(buffer);
imagesEnCache.put(key, bmp);
}
}
} |