我正在尝试将 base64 字符串转换为图像,但没有任何效果,我在 firebase 中从 ESP32-CAM 接收到这个 base64 字符串: 这是图像字符串here
这是代码:
databaseReference.child("esp32-cam-data").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
try {
String data = snapshot.getValue().toString();
String img = data.split(",")[1];
Log.i("Image data", img);
Bitmap bm = stringToBitMap(img);
cameraPreview.setImageBitmap(bm);
}catch(Exception e){
Log.e("Image Exeption", e.toString());
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
public Bitmap stringToBitMap(String encodedString) {
try {
byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
} catch (Exception e) {
e.getMessage();
return null;
}
}
2024-03-15 04:24:31.356 29923-29923 skia com.nidcam.nidcam D --- Failed to create image decoder with message 'unimplemented'
我尝试更改和使用Picasso和Glide -与毕加索:
try {
String data = snapshot.getValue().toString();
Picasso.get()
.load(data)
.into(cameraPreview);
}catch(Exception e){
Log.e("Image Exeption", e.toString());
}
-带滑行:
try {
String data = snapshot.getValue().toString();
Glide.with(getApplicationContext())
.load(data)
.into(cameraPreview);
}catch(Exception e){
Log.e("Image Exeption", e.toString());
}
什么也没用,请帮帮我。
正如我看到你的代码,我认为你的代码没问题,但你的base64编码是错误的,我从未见过包含的base64
% 符号
base64 仅包含
(A-Z、a-z、0-9、+、/)
使用正确的 Base64 编码重新编码您的 Base64 数据
希望有帮助!