If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
brendanmc25
Posts: 63
Registered: ‎08-07-2009
My Device: Not Specified

Saving and retriveing images - rms

Could somebody please have a look at these 2 methods to see where i am going wrong. It was a helper class from here - http://developers.sun.com/mobility/midp/ttips/imagesinrms/ but i have amended a few lines in order to save and retrieve a Bitmap rather than an Image. Unfortunatley I am not getting viewable pictures although the load method is not returning null.

 

I am a little out of my depth with int and bytes[] when it comes to images but i really could do with  my app having the ability to store some images, rather than download them time and again.

 

I would really appreciate a little glance at the code.

 

Regards

 

Brendan

 

public static  void savePngImage(String recordStore, String resourceName, Bitmap image) {
         Log.info("SAVING " + recordStore + resourceName);
        RecordStore imagesRS = null;
        int height, width;
        if (resourceName == null) return; // resource name is required

        // Calculate needed size and allocate buffer area
        height = image.getHeight();
        width = image.getWidth();

        int[] imgRgbData = new int[width*height];

        try {

            image.getARGB(imgRgbData, 0, width, 0, 0, width, height);
            imagesRS = RecordStore.openRecordStore(recordStore, true);

            //
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            DataOutputStream dout = new DataOutputStream(bout);
            //  Serialize the image name
            dout.writeUTF(resourceName);
            dout.writeInt(width);
            dout.writeInt(height);
            dout.writeLong(System.currentTimeMillis());
            dout.writeInt(imgRgbData.length);
            //  Serialize the image raw data
            for (int i=0; i<imgRgbData.length; i++) {
                dout.writeInt(imgRgbData[i]);
            }
            dout.flush();
            dout.close();
            byte[] data = bout.toByteArray();
            int recid = imagesRS.addRecord(data, 0, data.length);
        } catch (Exception e) {
            // Log the exception
        } finally {
            try {
                Log.info("SAVING numbee " + imagesRS.getNumRecords());
                // Close the Record Store
                if (imagesRS != null) imagesRS.closeRecordStore();
            } catch (Exception ignore) {
                // Ignore
            }
        }


    }

    /**
     * Load image with specified name
     *
     * @param recordStore is the name of the record store.
     * @param resourceName is the name of the image to load
     *
     * @return the loaded Image or null.
     */
    static public Bitmap loadPngFromRMS(String recordStore, String resourceName) {
        Log.info("LOADING " + recordStore + resourceName);
        RecordStore imagesRS = null;
        Image img = null;
        Bitmap bb = null;
        try {
            imagesRS = RecordStore.openRecordStore(recordStore, true);
            RecordEnumeration re = imagesRS.enumerateRecords(null, null, true);
            int numRecs = re.numRecords();
            // For each record
            for(int i=0; i<numRecs; i++) {
                // Get the next record's ID
                int recId = re.nextRecordId(); // throws InvalidRecordIDException
                // Get the record
                byte[] rec = imagesRS.getRecord(recId);
                //
                ByteArrayInputStream bin = new ByteArrayInputStream(rec);
                DataInputStream din = new DataInputStream(bin);
                String name = din.readUTF();
                // If this is the image we are looking for, load it.
                if (name.equals(resourceName)== false) continue;

                int width  = din.readInt();
                int height = din.readInt();
                long timestamp = din.readLong();
                int length = din.readInt();

                int[] rawImg = new int[width*height];
                //  Serialize the image raw data
                for (i = 0; i < length; i++) {
                    rawImg[i] = din.readInt();
                }
                 bb = new Bitmap(width, height);
                bb.setARGB(rawImg, 0, 0, 0, 0, width, height);
                //img = Image.createRGBImage(rawImg, width, height, false);
                din.close();
                bin.close();
            }
        } catch (InvalidRecordIDException ignore) {
            // End of enumeration, ignore
        } catch (Exception e) {
            // Log the exception
        } finally {
            try {
                // Close the Record Store
                if (imagesRS != null) imagesRS.closeRecordStore();
            } catch (Exception ignore) {
                // Ignore
            }
        }

        Log.info("Loading numbee " + bb.toString());
        return bb;
    }

 

Please use plain text.
Developer
martijnbrinkers
Posts: 155
Registered: ‎02-18-2009
My Device: Not Specified

Re: Saving and retriveing images - rms

"...where i am going wrong" .. well for one you do not give us enough information to get started with :smileywink: ... "going wrong" is too broad. What's exactly the problem? Exceptions? your BB explodes when you run it?

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Saving and retriveing images - rms

Your call to setARGB looks wrong. For one thing, you're passing a scan length of 0, which can't be right.

 

Your RMS code looks fine to me, but if you want to be sure, why not do a round-trip test? Save the image data to RMS, retrieve it, and check that you got back what you started with. That will decouple the RMS code from the Bitmap code and let you narrow down the problem.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
New Developer
touristguy87
Posts: 6
Registered: ‎11-01-2009
My Device: Not Specified

Re: Saving and retriveing images - rms

Hi, did you ever get this to work, I'm very interested in getting pre NR image data out of my 8320...especially if this development can be done in netbeans on a ubuntu box.

 

thanks

Please use plain text.