1 Reply Latest reply: May 28, 2010 1:53 AM by 807581 RSS

    Cannot Delete RecordStore although RecordStore is closed already

    807581
      Hi... my class has 2 methods which 1 of them saves image to RecordStore and the other one loads the saved image from RecordStore. In some conditions, some of the saved images need to be deleted from the RecordStore.
      Well, i keep getting "javax.microedition.rms.RecordStoreException: deleteRecordStore error: record store is still open". I'm pretty sure i have closed the RecordStore but it just can't be deleted.
      Below is the excerpt of my code. Since my original code is very long, i just enter the 3 relevant methods. Please kindly help. :)
        //A function to load image from the memory
          public Image LoadImage(int width, int height, String imageCid) throws RecordStoreException {
            rs = RecordStore.openRecordStore(imageCid, true);
      
              byte[] b = null;
              String imagename = null;
              Image image = null;
      
              if (rs.getNumRecords() > 0) {
      
                  try {
      
                      int i = rs.getNumRecords();
      
                      for(int j = 1; j < i + 1; j++) {
      
                          if(rs.getRecord(j) != null) {
      
                              b = rs.getRecord(j);
                              ByteArrayInputStream  bin =
                                  new ByteArrayInputStream( b );
      
                              DataInputStream   din = new DataInputStream( bin );
      
                              imageCid = din.readUTF();
                              int remaining =
                                  (b.length-imageCid.getBytes().length-2)/4;
      
                              int[] rawdata = new int[remaining];
      
                              for(int k =0 ;k<rawdata.length ;k++) {
                                  rawdata[k] = din.readInt();
                              }
      
                              image = Image.createRGBImage(rawdata,
                                  width, height, false);
      
                              bin.reset();
                              din.close();
                              din =null;
                          }
                      }
                  } catch (IOException e) {
      
                      e.printStackTrace();
      
                  } catch(RecordStoreException recordstoreexception) {
      
                      recordstoreexception.printStackTrace();
      
                  }
                  finally {
                      System.out.println("Banner LoadImage - closing RecordStore of " + imageCid);
                       rs.closeRecordStore();
                       rs = null;
              
             
                  }
              }
              return image;
          }
      
      //A function to save image to the memory of the device
          public boolean SaveImage (Image img, int width,
                  int height, String imageCid) throws RecordStoreException {
                rs = RecordStore.openRecordStore(imageCid, true);
      
              if (img == null || width < 0 || height < 0 || imageCid == null) {
      
                  throw new IllegalArgumentException("Check arguments");
      
              }
      
              int[] imgRgbData = new int[width * height];
      
              try {
      
                  img.getRGB(imgRgbData, 0, width, 0, 0, width, height);
      
              } catch (Exception e) {
                  // Problem getting image RGB data
                  return false;
              }
              try {
                  // Write image data to output stream (in order to get
                  // the record bytes in needed form)
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  DataOutputStream dos = new DataOutputStream(baos);
                  dos.writeUTF(imageCid);
      
                  for (int i = 0; i < imgRgbData.length; i++) {
                      dos.writeInt(imgRgbData);
      }

      // Open record store, create if it doesn't exist
      rs.addRecord(baos.toByteArray(), 0,
      baos.toByteArray().length); // Add record

      } catch (RecordStoreNotFoundException rsnfe) {
      // Record storage not found
      System.out.println("Banner SaveImage - RecordStoreNotFound");
      System.out.println("Banner SaveImage - closing RecordStore of " + imageCid);

      rs.closeRecordStore();
      rs = null;

      return false;
      } catch (RecordStoreException rse) {
      // Other record storage problem
      System.out.println("Banner SaveImage - RecordStoreNotException");
      System.out.println("Banner SaveImage - closing RecordStore of " + imageCid);

      rs.closeRecordStore();
      rs = null;

      return false;
      } catch (IOException ioe) {
      // Problem writing data
      System.out.println("Banner SaveImage - IOException");
      System.out.println("Banner SaveImage - closing RecordStore of " + imageCid);

      rs.closeRecordStore();
      rs = null;

      return false;
      }
      System.out.println("Banner SaveImage - closing RecordStore of " + imageCid);
      rs.closeRecordStore();
      rs = null;


      System.out.println("Banner SaveImage - OK");
      return true; // We've successfuly done
      }

      //Delete stored image based on campaignId
      private void DeleteStoredImage (String imageCid) throws RecordStoreException {

      if (RecordStore.listRecordStores() != null) {
      RecordStore.deleteRecordStore(imageCid);
      }

      }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        • 1. Re: Cannot Delete RecordStore although RecordStore is closed already
          807581
          I don't know if it's still relevant, but cut there be a situation where you open the recordstore and don't close it? Because when you open the same recordstore twice and close it just one time then you can get this exception... I see you open the recordstore outside the try block and close it in the finally block. so just looking at your if condition makes it pretty clear that when you have no records in your recordstore and you try to load the images, then the RMS doesnt get closed... and when you then try to delete it, it will give you the RecordStoreException... So best would be to put the try also around the openrecordstore method