When trying to set a byte parameter of type [] in Google Endpoints object, String is checked

I have created an object that is submitted via Google Cloud Endpoints. Its parameters include one parameter of type byte [] with its getter and setter methods.

The problem is when I want to set a byte [] parameter in my application, does it expect a String parameter somehow? Does he want me to encode a byte array or what is the reason for this strange error?

When I get the parameter from the dataObject it is of type [] byte and everything is fine. I'm really confused!

My object is a DataPackage defined in my backend

 ...
 private byte[] imageThumbnail;

 public void setImageThumbnail(byte[] imageThumbnail) {
    this.imageThumbnail = imageThumbnail;
}

public byte[] getImageThumbnail() {
    return imageThumbnail;
}

      

Then in my application I try to set the imageByteArray

 dataPackage.setImageThumbnail(byteThumbnail); // gives error that String is expected

      

And it works great

 dataPackage.getImageThumbnail() // is of type byte[]

      

+3


source to share


2 answers


While this may not be your first choice, can you use the byte [] constructor on a string and pass it in? An example of how to do this can be found here

Essentially, the relevant part of this question / answer is this:

byte[] b = {(byte) 99, (byte)97, (byte)116}; //This could also be your byte array
String s = new String(b, "US-ASCII"); // US-ASCII might need to be something else

      



and then the opposite process:

String s = "some text here"; //The string from dataPackage.getImageThumbnail()
byte[] b = s.getBytes("UTF-8"); //Might need to be some other byte formatting 

      

Happy coding! Leave a comment if you have questions.

+2


source


You have to decode and encode using the Base64 class from the Android framework.

String encodedBitArray = Base64.encodeToString(imageThumbnail, Base64.DEFAULT);

byte[] decodedArray = Base64.decode(encodedBitArray, Base64.DEFAULT);

      

If you want to convert a byte array to Bitmap, you need to do the following:



Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

      

SO example>

-1


source







All Articles