首页 \ 问答 \ android位图不是从base64创建的(android bitmap isn't created from base64)

android位图不是从base64创建的(android bitmap isn't created from base64)

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

我做了一个测试程序来比较从Android发送到服务器的base64数据和从服务器发送回Android的base64 - 它们完全相同。

我想使用base 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");}

我一直都是null,因为日志只打印“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);

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

原文:https://stackoverflow.com/questions/16966656
更新时间:2023-08-15 12:08

最满意答案

因为语言标准的第6.4.4.2节“浮动常量”因此定义它们:

floating-constant:
    decimal-floating-constant
    hexadecimal-floating-constant

decimal-floating-constant:
    fractional-constant exponent-part(opt) floating-suffix(opt)
    digit-sequence exponent-part floating-suffix(opt)

hexadecimal-floating-constant:
    hexadecimal-prefix hexadecimal-fractional-constant
    binary-exponent-part floating-suffixopt
    hexadecimal-prefix hexadecimal-digit-sequence
    binary-exponent-part floating-suffix(opt)

fractional-constant:
    digit-sequence(opt) . digit-sequence
    digit-sequence .

exponent-part:
    e sign(opt) digit-sequence
    E sign(opt) digit-sequence

sign: one of
    + -

digit-sequence:
    digit
    digit-sequence digit

hexadecimal-fractional-constant:
    hexadecimal-digit-sequence(opt) .
    hexadecimal-digit-sequence
    hexadecimal-digit-sequence .

binary-exponent-part:
    p sign(opt) digit-sequence
    P sign(opt) digit-sequence

hexadecimal-digit-sequence:
    hexadecimal-digit
    hexadecimal-digit-sequence hexadecimal-digit

floating-suffix: one of
    f l F L

重要的是,以下所有内容都是有效的浮点文字,意思是“零”:

0。

0.0

0.0

(你的“100”将是一个有效的floating-constant ,因为它是一个decimal-floating-constant由一个fractional-constant decimal-floating-constant组成(省略了可选的exponent-partfloating-suffix );它是一个digit-sequence后跟一个句点,对于一个fractional-constant有效,如该noteable定义的第二行。)


Because section 6.4.4.2 of the language standard, "Floating constants" , defines them thusly:

floating-constant:
    decimal-floating-constant
    hexadecimal-floating-constant

decimal-floating-constant:
    fractional-constant exponent-part(opt) floating-suffix(opt)
    digit-sequence exponent-part floating-suffix(opt)

hexadecimal-floating-constant:
    hexadecimal-prefix hexadecimal-fractional-constant
    binary-exponent-part floating-suffixopt
    hexadecimal-prefix hexadecimal-digit-sequence
    binary-exponent-part floating-suffix(opt)

fractional-constant:
    digit-sequence(opt) . digit-sequence
    digit-sequence .

exponent-part:
    e sign(opt) digit-sequence
    E sign(opt) digit-sequence

sign: one of
    + -

digit-sequence:
    digit
    digit-sequence digit

hexadecimal-fractional-constant:
    hexadecimal-digit-sequence(opt) .
    hexadecimal-digit-sequence
    hexadecimal-digit-sequence .

binary-exponent-part:
    p sign(opt) digit-sequence
    P sign(opt) digit-sequence

hexadecimal-digit-sequence:
    hexadecimal-digit
    hexadecimal-digit-sequence hexadecimal-digit

floating-suffix: one of
    f l F L

Bottom line, all of the following would be valid floating point literals meaning "zero":

0.

.0

0.0

(Your "100." would be a valid floating-constant, as it is a decimal-floating-constant consisting of a fractional-constant (omitting the optional exponent-part and floating-suffix); it is a digit-sequence followed by a period, which is valid for a fractional-constant as by the second line of that noteable's definition.)

相关问答

更多

相关文章

更多

最新问答

更多
  • 使用通配符获取更多servlet请求变量[重复](Get more servlet request variables using wildcards [duplicate])
  • 返回相同的集合类型,参数化不同(Returning same collection type, differently parameterised)
  • C ++朋友函数模板重载和SFINAE在clang ++,g ++,vc ++中的不同行为(C ++ 14模式)(C++ friend function template overloading and SFINAE different behaviors in clang++, g++, vc++ (C++14 mode))
  • 与paure IoT-Hub的Python paho-MQTT连接(Python paho-MQTT connection with azure IoT-Hub)
  • 编译器警告“来自不同的Objective-C类型的赋值”(Compiler warning “assignment from distinct objective-c type”)
  • C ++编译错误(在此函数中未初始化)[重复](C++ Compile Error (uninitialized in this function) [duplicate])
  • unsigned-signed下溢机制(unsigned-signed underflow mechanism)
  • 快速行查询的数据结构?(Data structure for fast line queries?)
  • 饥荒有手机安卓版的吗
  • Jquery可拖动碰撞检测错误(Jquery draggable collision detection bug)
  • sql调优是怎样来实现的?
  • 无法使占位符输入文本消失(Unable to make the placeholder input text disappear)
  • jQuery改变了两个div的CSS属性(JQuery change CSS property of two div's)
  • JDK中包含的库版本(Versions of libraries included in the JDK)
  • 请问下载的是出现ASP是什么意思
  • Firebase MLkit用于数字液晶显示器的文本识别(Firebase MLkit Text recognition for digital lcd displays)
  • 我可以在任何平台上运行C和C ++吗?(Can I run C and C++ on any platform?)
  • 让小组在C#的特定位置(get panel at specific positions in C#)
  • Nagios为通知设置了更高的间隔(Nagios set higher interval for notifications)
  • 无法向SMTP主机发送电子邮件(unable to send an email to SMTP host)
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何在.NET代码中验证全球邮政编码(How can I validate worldwide postal codes in my .NET code)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • Clojure:减少大型懒惰收集会占用内存(Clojure: Reducing large lazy collection eats up memory)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • 显示作为字符串的SVG(Showing an SVG that I have as a string)
  • 从jansson库里创建json请求的自由内存的正确方式是什么?(what is the proper way of free memory in creating json request from jansson libary?)
  • jQuery插件无法正常工作 - 它是附加的(jQuery plugin not working - it's appended)
  • 使用stat_summary自动调整ylim(Automatically adjusting ylim with stat_summary)