简体   繁体   English

不是从base64创建android位图

[英]android bitmap isn't created from base64

I have an Android application which sends an image to a web service. 我有一个Android应用程序,它将图像发送到Web服务。 I want to send the same photo back from the web service to Android. 我想将同一张照片从Web服务发送回Android。

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. 我编写了一个测试程序来比较从Android发送到服务器的base64数据和从服务器发送到Android的base64数据-它们是完全相等的。

I want to use the base 64 string to create a bitmap, so I tried this: 我想使用基数为64的字符串创建位图,因此我尝试了以下操作:

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". 由于日志仅显示“是”,因此我一直为null。

Can anyone please help? 谁能帮忙吗?

If you want to know how I encode the image it is as follows: 如果您想知道我如何编码图像,则如下所示:

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);

Try this to bitmap; 试试这个位图;

public Bitmap convert(String img){
    byte[] b = Base64.decode(img, Base64.DEFAULT);
    return BitmapFactory.decodeByteArray(b, 0, b.length);
}

And this to String 这给弦乐

    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. 确实,您的代码没有真正的问题,但是这些问题对我有用,因此我建议您尝试一下,看看这是否真的是您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM