不是从base64创建android位图 [英] android bitmap isn't created from base64
问题描述
我有一个Android应用程序,它将图像发送到Web服务.我想将同一张照片从Web服务发送回Android.
I have an Android application which sends an image to a web service. I want to send the same photo back from the web service to Android.
我制作了一个测试程序,以比较从Android发送到服务器的base64数据和从服务器发送到Android的base64数据-它们是完全相等的.
I made a test program to compare the base64 data that's sent from Android to the server and the base64 that's sent back from server to Android -- they are exactly equal.
我想使用基数为64的字符串来创建位图,因此我尝试了以下操作:
I want to use the base 64 string to create a bitmap, so I tried this:
String image = client1.getBaseURI("restaurantFoods/OneFood/"
+ this.getID() + "/getImage");
byte[] decodedString = Base64.decode(image, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
if(decodedByte == null){
Log.d(this.getFoodItem().getName(), image);
Log.d("isNull", "Yes");
}
else{
Log.d("isNull", "No");}
由于日志只显示是",所以我一直为空.
I keep getting null because the log just prints "YES".
任何人都可以帮忙吗?
如果您想知道如何对图像进行编码,如下所示:
private String getBase64(Bitmap bitmap) {
String imgString = Base64.encodeToString(getBytesFromBitmap(bitmap),
Base64.NO_WRAP);
return imgString;
}
private byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.pizza);
String iconBase64 = this.getBase64(icon);
推荐答案
尝试使用此位图;
public Bitmap convert(String img){
byte[] b = Base64.decode(img, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
这是字符串
public String convert(Bitmap bm, int quality){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, quality, baos);
byte[] byt = baos.toByteArray();
bm.recycle();
return Base64.encodeToString(byt, Base64.DEFAULT);
}
真的,我看不到您的代码有任何实际问题,但是这些问题对我有用,因此我建议您尝试一下,看看这是否真的是您的问题.
Really I don't see any real problems with your code, but these have worked for me so I suggest that you try them and see if that is actually your problem.
这篇关于不是从base64创建android位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!