Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified
Accepted Solution

Retrieve image bytes from image file path

Hello,

 

I've been unable to find a post that details how to retrieve the image bytes from an image (given the file path of the image on the device).  Basically, using the FilePicker, I'm able to get the path of an image that the user chooses:

 

 

file:///store/samples/pictures/Bridge.png

 

 

Now, I'm looking to get the bytes for this image, so that I can send it to a server.  Can anyone provide me with some sample code, or perhaps refer me to another post?

 

Thank you.

Developer
Posts: 466
Registered: ‎03-04-2009
My Device: 8520

Re: Retrieve image bytes from image file path

There you go:

just pass your filename as the method parameter.

 

 public static byte[] getBytesFromFile(String filename) throws IOException {
        FileConnection fconn = null;
        InputStream is = null;
        try {
            fconn = (FileConnection) Connector.open(filename, Connector.READ);
            is = fconn.openInputStream();

            return IOUtilities.streamToBytes(is);
        } finally {
            if (is != null) {
                is.close();
            }
            if (fconn != null) {
                fconn.close();
            }
        }
    }

 

 

 

 

Got resolved! Press kudo icon!
Developer
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Retrieve image bytes from image file path

Perfect, I appreciate your help!  Thanks!

Developer
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Retrieve image bytes from image file path

Another question:

 

Is filename meant only to be the filename (image.png), or can it be a path (as I showed as an example in my original post)?

 

The reason that I ask is because here is the output of the bytes from an example image:

 

[B@81a6843f

 

That looks more like a pointer to me than actual bytes describing an image, no?

Developer
Posts: 466
Registered: ‎03-04-2009
My Device: 8520

Re: Retrieve image bytes from image file path

the path name only applies

Got resolved! Press kudo icon!