将 Bitmap 对象转换为存在问题的 Base64 字符串
我有一个简单的应用程序,其中从相机抓取图像,然后传递给我的 onActivityResult() 方法。但是,我无法将位图对象编码为 Base64 字符串。 Eclipes 告诉我,行 byte[] returned Image = Base64.encode(b, Base64.DEFAULT);
应该是 byte[] 而不是 String,所以这就是我认为问题所在(因此它下面的行试图强制它作为字符串对象)。我的代码如下,该方法被触发并出现日志,但数据不是base64。
任何人都可以帮助我吗?
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch(requestCode){
case TAKE_PHOTO_CODE:
if( resultCode == RESULT_OK ){
Bitmap thumbnail = (Bitmap) intent.getExtras().get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
byte[] encodedImage = Base64.encode(b, Base64.DEFAULT);
String encodedImageStr = encodedImage.toString();
Log.e("LOOK", encodedImageStr);
}
// RESULT_CANCELED
break;
}
}
I have a simple application where a image is grabbed from the camera, then passed to my onActivityResult() method. I however can't encode the bitmap object into a base64 string. Eclipes tellsi me that the line byte[] encodedImage = Base64.encode(b, Base64.DEFAULT);
should be a byte[] instead of a String, so this is where i think the issue is (hence the line below it trying to force it as a string object). My code is below, this method gets triggered and the Log appears, but the data is NOT base64.
Can anyone help me please.
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch(requestCode){
case TAKE_PHOTO_CODE:
if( resultCode == RESULT_OK ){
Bitmap thumbnail = (Bitmap) intent.getExtras().get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
byte[] encodedImage = Base64.encode(b, Base64.DEFAULT);
String encodedImageStr = encodedImage.toString();
Log.e("LOOK", encodedImageStr);
}
// RESULT_CANCELED
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数组的内容执行任何操作
数组对象的 toString 不对您应该使用的
,或者您可以直接转到 String
the toString of array object don't do anything with the contents of the array
you should use
or you can go directly to String with