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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
behrk2
Posts: 364
Registered: ‎11-25-2009
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.

Please use plain text.
Developer
sonicboomboy
Posts: 465
Registered: ‎03-04-2009
My Carrier: some

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!
Please use plain text.
Developer
behrk2
Posts: 364
Registered: ‎11-25-2009

Re: Retrieve image bytes from image file path

Perfect, I appreciate your help!  Thanks!

Please use plain text.
Developer
behrk2
Posts: 364
Registered: ‎11-25-2009

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?

Please use plain text.
Developer
sonicboomboy
Posts: 465
Registered: ‎03-04-2009
My Carrier: some

Re: Retrieve image bytes from image file path

the path name only applies

Got resolved! Press kudo icon!
Please use plain text.