该应用程序将base64解码为PNG,但是当我将文件编码回base64以便发送到服务器时,生成的base64是不同的,并且不会生成图像 .
这是原始base64字符串的开头: /9j/4AAQSkZJRgABAQAASABIAAD/4QBMRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAYAAIdp
这是从base64和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);
}