On android which class corresponds to javafx Image class

Asked

Viewed 47 times

0

I have image server implemented in javaEE, in javafx I make an image request to the server that returns an array of bytes, with this result create the image on the client side:

** Image in PNG format.

byte buffer[] -> image bytes retornado pelo servidor

// no javaFX    
Image image = new Image(new ByteArrayInputStream(buffer));  

How do I create the image from the byte array on android?

On the server I read the image as follows:

private byte[] readImage(String imageName) throws FileNotFoundException
{
    File fle = new File(imageName); 
    FileInputStream new FileInputStream(fle);   

    byte buf[] = new byte[(int) fle.length()];

    rea.read(buf,0,buf.length);         

    rea.close();

    return buf;                                 
}

1 answer

2


The class used on Android to work with images whose format represents a map of bits is Bitmap.

Having the array of bytes use the method decodeByteArray() class Bitmapfactory to decode the array in a Bitmap.

Use like this:

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

It is possible to resize the image while decoding an object Bitmapfactory.Options to the method decodeByteArray(), see in this reply as.

  • I tested and Bitmapfactory returns null. I imagine the error is not in the byte array returned by the server because in javafx the image is created correctly. Is there any other secret?

  • As far as I know there is no secret anymore. How are you encoding the byte array?

  • I edited the question and added the reading method to the server.

  • 1

    I don’t know what the problem will be. I can only say that if the byte array is a valid representation of a JPEG or PNG method BitmapFactory.decodeByteArray() will decode to a Bitmap.

  • 1

    The problem was in PNG. I saved the image as JPG and Code worked. I edited the PNG and saved the Code worked. **** The image I was using as a test I downloaded from https://design.google.com/icons/.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.