【发布时间】:2017-04-02 05:11:47
【问题描述】:
应用程序将 base64 解码为 PNG,但是当我将文件编码回 base64 以便发送到服务器时,生成的 base64 是不同的并且不会生成图像。
这是原始 base64 字符串的开头:
/9j/4AAQSkZJRgABAQAASABIAAD/4QBMRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAYAAIdp
这是从 PNG 文件编码后的 base64 的开头:
iVBORw0KGgoAAAANSUhEUgAAD8AAAAvQCAIAAABPl1n3AAAAA3NCSVQICAjb4U/gAAAgAElEQVR4nO
这是我用来将文件编码为 base64 的代码:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = true;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
File file = new File(root +"/saved_images/"+note.imageLocation );
if(file.exists()){
// TODO perform some logging or show user feedback
try {
Bitmap myBitmap = BitmapFactory.decodeFile(root +"/saved_images/"+note.imageLocation, options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
JSONObject object =new JSONObject();
object.put("image_type", "image/png");
object.put("image_data", Base64.encodeToString(byteArray, Base64.DEFAULT));
if (note.serverID == -1) {
toReturn.put(String.valueOf(i), object);
}else{
toReturn.put(String.valueOf(note.serverID), object);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i--;
}else{
Log.i("File Not Found", "NoteModel - "+file);
}
【问题讨论】:
-
The app decodes base64 to PNG。您应该开始向我们展示该代码。而且您也没有告诉该 png 保存在哪里。您是否将其保存为文件?
标签: android encoding bitmap base64