Android的位图不是从BASE64创建 [英] android bitmap isn't created from base64

查看:164
本文介绍了Android的位图不是从BASE64创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,发送图像到Web服务。我想同一张照片从Web服务到Android送回。

我做了一个测试程序来比较这与Android的发送到服务器而这送回从服务器到Android中的Base64 Base64编码的数据 - 他们是完全相等

我想使用基本的64字符串创建位图,所以我尝试这样的:

 字符串图像= client1.getBaseURI(restaurantFoods / OneFood /
            + this.getID()+/的getImage);

byte []的德codedString = Base64.de code(图像,Base64.DEFAULT);
        位图德codedByte = BitmapFactory.de codeByteArray(德codedString,0,
                德codedString.length);
如果(德codedByte == NULL){
            Log.d(this.getFoodItem()的getName(),图像);
            Log.d(的isNull,是);
        }
        其他{
            Log.d(的isNull,否);}
 

我不断收到空,因为日志只是打印YES。

任何人都可以请帮助?

如果你想知道我怎么连接code中的图像是如下:

 私人字符串getBase64(位图位图){
        字符串imgString = Base64.en codeToString(getBytesFromBitmap(位图)
                Base64.NO_WRAP);
        返回imgString;
    }
私人字节[] getBytesFromBitmap(位图位图){
        ByteArrayOutputStream流=新ByteArrayOutputStream();
        bitmap.com preSS(比较pressFormat.JPEG,70,流);
        返回stream.toByteArray();
    }
位图图标= BitmapFactory.de codeResource(this.getResources()
                    R.drawable.pizza);
字符串iconBase64 = this.getBase64(图标);
 

解决方案

试试这个位图;

 公共位图转换(字符串IMG){
    byte []的B = Base64.de code(IMG,Base64.DEFAULT);
    返回BitmapFactory.de codeByteArray(B,0,b.length个);
}
 

和这串

 公共字符串转换(位图BM,诠释品质){
    ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
    bm.com preSS(Bitmap.Com pressFormat.JPEG,质量,BAOS);

    byte []的BYT = baos.toByteArray();
    bm.recycle();
    返回Base64.en codeToString(BYT,Base64.DEFAULT);
}
 

我真的没有看到任何真正的问题,您的code,但这些对我来说都工作过,所以我建议你尝试一下,看看是否实际上是你的问题。

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.

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.

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".

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.

这篇关于Android的位图不是从BASE64创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆