gpt4 book ai didi

java - Bad Base - 尝试发送图像时出现 64 错误

转载 作者:行者123 更新时间:2023-12-02 03:58:41 28 4
gpt4 key购买 nike

我希望在两个设备之间共享页面的屏幕截图。屏幕截图存储在位图中,然后我将其转换为字节数组,然后将其转换为 Base 64 的字符串。然后将该字符串发送到将显示图像的处理程序。在我尝试解码图像后,它给了我一个: java.lang.IllegalArgumentException: bad base-64

我已经尝试过以不同的方式发送图像,并尝试了所有不同的 Base 64 方法,例如 Base64.DEFAULT、URLSAFE、NOPADDING 等...

这是我创建屏幕截图并发送的位置:

Bitmap b = 
Screenshot.takescreenshotofRootView(MainActivity.imageView);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG,100,baos);
byte[] bytea = baos.toByteArray();
String temp = Base64.encodeToString(bytea, Base64.DEFAULT);
sendReceive.write(temp.getBytes());

这是我处理数据的地方

Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_READ:
MainActivity.imageView.setImageBitmap(bitm);
byte[] readBuff = (byte[]) msg.obj;
String tempMsg = new String(readBuff, 0, msg.arg1);
byte [] encodeByte = Base64.decode(tempMsg,Base64.URL_SAFE);
Bitmap bitms
= BitmapFactory.decodeByteArray(encodeByte,0,encodeByte.length);
MainActivity.imageView.setImageBitmap(bitms);
break;
}
return true;
}
});

最佳答案

您可能会在没有意识到的情况下遇到 OutOfMemoryError,从而使您的 Base64 字符串无效。您应该用 try/catch block 包装代码以查看是否发生这种情况。这是我的一些代码中的示例:

    String imageString = "";

try {
if (this.theBitmap != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
this.theBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bytes = stream.toByteArray();
imageString = Base64.encodeToString(bytes, Base64.DEFAULT);
}

}catch(OutOfMemoryError E){
Log.e("MyApp", "Out Of Memory error");
}

关于java - Bad Base - 尝试发送图像时出现 64 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56744288/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com