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: 445
Registered: ‎10-14-2008
My Device: Blackberry Bold 9000
My Carrier: SingTel

Image from File System

Hello,

 

I want to show an image on Dialog from file system.

 

Here is my code :

 

try {
                                
                                FileConnection fc = (FileConnection)Connector.open(theFile, Connector.READ_WRITE);
                                
                                //InputStream is = (this.getClass()).getResourceAsStream(theFile);
                                byte[] data = getData(fc);
                                //is.read(data, 0, 5000); // read image
                                Bitmap b = Bitmap.createBitmapFromBytes(data, 0, data.length, 1);
                                Dialog d = new Dialog(Dialog.D_OK, "Image!!!!!!!!", Dialog.D_OK, b, 0);
                                d.show();

                            } catch (IOException e) {
                                e.printStackTrace();
                            }  

 

 

private byte[] getData(FileConnection file)
    {
        byte[] data = new byte[0];
        try {
            InputStream input = file.openInputStream(); //////////
            int SIZE = 100000;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[SIZE];
            while (true) {
                int bytesRead = input.read( buffer, 0, SIZE );
                if (bytesRead == -1)
                    break;
                byteArrayOutputStream.write( buffer, 0, bytesRead );
                }
            data = byteArrayOutputStream.toByteArray();
            byteArrayOutputStream.flush();
            byteArrayOutputStream.close();
            }
        catch(Exception e) {
            System.out.println(e.toString());
            }
        return data;
        }

 

Its giving me IlligalArgumentException......

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thanks & Regards
Zahid Naqvi | Senior Software Engineer
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Image from File System

Your getData look dubious, have a look at the following code I think is more efficient and more likely to do what you want.

 

Note, not production quality code, and I've just created it, so might not compile.  But you will get the idea.

 

    public final static byte [] readDataFile(String fileName) {
        InputStream input = null;
        FileConnection fconn = null;
        byte [] readBuffer = null;
        try {
            fconn = (FileConnection)Connector.open(fileName, Connector.READ);
            if (fconn.exists()) {
                long fileSize = fconn.fileSize();
                readBuffer = new byte [(int) fileSize];
                input = fconn.openInputStream();
                int readLen = input.read(readBuffer);
            }
        } catch (Exception e) {
            System.out.println("Exception in File Processing" +
                               ".\nError Exception: " + e.toString());
            readBuffer = null;
        } finally {
            try {
                if ( input != null ) {
                    input.close();
                    input = null;
                }
                if ( fconn != null ) {
                   fconn.close();
                   fconn = null;
                }
            } catch (Exception e) {
                System.out.println("Exception in file tidying : " + e.toString()); 
            }
        }
        return readBuffer;
    }

 

 

Highlighted
Developer
Posts: 73
Registered: ‎07-16-2009
My Device: Bold

Re: Image from File System

I share this code for the getData() method, I think it is more simple and clear Smiley Happy

 

 

try {
    FileConnection fconn = (FileConnection) Connector.open(filename);
    if (fconn.exists() && fconn.canRead()) {
        byte data[] = new byte[(int) fconn.fileSize()];
        DataInputStream input = fconn.openDataInputStream();
        input.readFully(data);

        input.close();
        fconn.close();

        return data;
    }else{
//file doesn't exists
}
} catch (IOException ieo) { }
return null;

 

 

 

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Image from File System

We can argue elements of style and use of different streams but they are not that important. 

 

What I would argue is important and a concern if using the code proposed by laliux, is the chance that the connection and the input stream might not be closed after an exception. 

 

The use of finally may be more complicated, but it provides the programmer with the opportunity to tidy up all the resources used in a way that is visible and should be understood by any Java programmer.  . 

 

And yes there is a hole in this regard in my sample code.  As noted, not production code. 

 

But this is just my opinion.   Everyone can make their own mind up!