【发布时间】:2015-08-15 06:42:29
【问题描述】:
编辑 2:从我在第一次编辑中得到的结果来看,在帖子的底部,问题出在这一行或以下的某处:imageEncodedSigneAgent = Base64.encodeToString(byteSigneAgent,Base64.NO_WRAP);"
/编辑。
我尝试在 MIME 消息中编写位图,作为 PNG 格式的 Base64 字符串。可悲的是,该消息被视为空(Windows 查看器无法读取它,说大小为 0kb)
我的代码:
Bitmap bitmapSigne = BitmapFactory.decodeFile(fileSigne.getAbsolutePath());
if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "createEMLInt - bitmapSigne width = " + bitmapSigne.getWidth());};
ByteArrayOutputStream baos = new ByteArrayOutputStream(bitmapSigne.getByteCount());
if (!bitmapSigne.compress(Bitmap.CompressFormat.PNG, 100, baos)) {
new TException(tagLocal, "Compress bitmapSign == ", "false");
}
byte[] byteSigne = baos.toByteArray();
if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "createEMLInt - byteSigne size = " + byteSigne.length);};
String imageEncodedSigne = Base64.encodeToString(byteSigne,Base64.NO_WRAP);
if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "createEMLInt - imageEncodedSigne size = " + imageEncodedSigne.length());};
mime.append(imageEncodedSigne);
mime.append("\r\n");
这确实在我的 MIMI 消息中写入了 base64 行,但它无法读取并且可能有点短。我认为它可能是 PNG 周围的元数据(从我解码后读到的内容)。
我测试过将位图直接保存到文件中,效果很好:
// test to retrieve BitMap as PNG
String signePath2 = Baseline.strSdExternalPath + File.separator + Params.MIF_REP;
File file = new File(signePath2, "sign.png");
FileOutputStream fOut = new FileOutputStream(file);
bitmapSigne.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
// test to retrieve BitMap as PNG
我正确地得到了我之前绘制的图像。
我也尝试过(来自另一个 SO 问题的想法:Converting bitmap to byte array, to string, then all the way back)将字符串重新转换为位图,但它失败了 :(
byte[] newImageBytes = imageEncodedSigne.getBytes();
// Convert byte[] back to bitmap
Bitmap bitmapReconverted = BitmapFactory.decodeByteArray(newImageBytes, 0, newImageBytes.length);
File fileReconverted = new File(signePath2, "signReConverted.png");
fOut = new FileOutputStream(fileReconverted);
bitmapReconverted.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
我在这里缺少什么?为什么我无法正确检索字节数据并在 base64 中获取可读字符串?
任何帮助不胜感激!
我之前使用 Base64.DEFAULT 没有更好的结果。我阅读了几篇文章,其中链接的一篇与我正在尝试做的最接近,遗憾的是他的问题是“equalTo”使用不当
编辑:所以我尝试做 Bitmap -> bytes[] -> Bitmap 并且它起作用了:
Bitmap bitmapReconverted = BitmapFactory.decodeByteArray(byteSigne, 0, byteSigne.length);
File fileReconverted = new File(signePath2, "signReConverted.png");
fOut = new FileOutputStream(fileReconverted);
bitmapReconverted.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
编辑 3:用于文档目的的 MIME 结果!
Content-Type: image/png
内容配置:表单数据;名称="正文";文件名="20150601.190623_00008_SIGN.PNG" 内容传输编码:base64 iVBORw0KGgoAAAANSUhEUgAAAOYAAABuCAIAAAC86/hZAAAA
【问题讨论】:
-
旁注:
new TException(...)显然没有做任何事情。你的意思可能是throw new TException(...) -
是的,是的。你不知道,但它是一个自定义的 Exception 包装器,可能不在那里调用 ''throw'' 方法是糟糕的设计。
-
这可能不是一个很好的设计。从发布的代码中不清楚,但可能是对象的创建没有用。此外,创建异常的子类有助于捕获它。
throw关键字会立即解释发生了什么。 -
你能发布你的日志吗?
-
您好,如果您认为这对您有帮助,我可以发布 MIME 消息。不抛出异常。检查我的编辑,问题不存在,它在代码下面的某个地方。
标签: java android bitmap base64 mime