Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7967675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:57:39+00:00 2026-06-04T06:57:39+00:00

I am trying to save a path to an image in the persistent store

  • 0

I am trying to save a path to an image in the persistent store to show the image when a user chooses a specific product from a history list(one image per product, taken by the user). Now the problem I am having is that when I load the app to the phone for the first time and try adding an entry to the persistent store, it throws an error a bit after and the app freezes. However when I come back, and add an image again, it works just fine and the images always load when I choose each specific product.

This is leading me to believe that the culprit is the first store8.commit() that I’m doing, for some reason it throws an exception : No Stack Trace, when debugging. Here is my code:

public class Storage extends Application {

private static final long PERSISTENT_KEY8 = 0x2c4c45c139ee9728L;
static PersistentObject store8 = PersistentStore.getPersistentObject(PERSISTENT_KEY8);
private static Vector pics;

/**
 * Picture Section ***********************************************************
 */
public static void savePicture(){

    store8.setContents(new Vector());
    store8.commit();

    if(pics == null){
        pics = new Vector();
    }
    synchronized(store8) {

        store8.setContents(pics); 
        store8.commit();
    }

}
public static String getPicture(String productName){

    if(pics.size()==0){
        return "";
    }else{
        for(int i = 0; i < pics.size(); i++){
            Pics product = (Pics)pics.elementAt(i);
            if(product.getProductName().equals(productName)){
                return product.getPic();
            }
        }
        return "";
    }   
}
public static void removePicture(String productName){

    if(pics.size()==0){
        return;
    }else{
        for(int i = 0; i < pics.size(); i++){
            Pics product = (Pics)pics.elementAt(i);
            if(product.getProductName().equals(productName)){
                pics.removeElementAt(i);
            }
        }
    }   
}   
public static void loadPicture(){

    pics = (Vector)store8.getContents();

    if(pics == null){
        pics = new Vector();
    }
}
public static void setPicture(Pics pro){
    if(pics.size()!=0){
        for(int j = 0; j< pics.size() ; j++){
            Pics product = (Pics)pics.elementAt(j);
            if(pro.getProductName().equals(product.getProductName())){
                pics.removeElementAt(j);
            }
        }
    }
    pics.addElement(pro);
}
}

So that is the class that contains my methods to save the image. Now for the other class where I am manipulating it:

public class ProductImage extends MainScreen implements FieldChangeListener,      AppLaunchResource {

private ImageButtonField logo;
private ButtonField newImage, chooseExisting;
public static BitmapField takenPicture;
//public static String picPath =""; 
private String currentPicture = "";
private String currentProduct ="";

public ProductImage(String productName){
    super(VERTICAL_SCROLL|VERTICAL_SCROLLBAR);
    currentProduct = productName;
    createGUI(); 
}
public void createGUI(){
    deleteAll();
    this.setTitle(new LabelField("Add An Image", Field.FIELD_HCENTER));
    if(ToolbarManager.isToolbarSupported())
    {
        Toolbar tb = new Toolbar();
        setToolbar(tb.createToolBar());
    }
    else{
        Toolbar tb = new Toolbar();
        add(tb.createNavBar());
    }

    try{
        Storage.loadPicture();
    }catch(NullPointerException e){
        e.printStackTrace();
    }


    newImage = new ButtonField("Take Photo", ButtonField.CONSUME_CLICK){
        public int getPreferredWidth() {
            return (int) (net.rim.device.api.system.Display.getWidth());
        }
    };
    chooseExisting= new ButtonField("Change Image", ButtonField.CONSUME_CLICK){
        public int getPreferredWidth() {
            return (int) (net.rim.device.api.system.Display.getWidth());
        }
    };

    newImage.setChangeListener(this);
    chooseExisting.setChangeListener(this);

    EncodedImage enc = EncodedImage.getEncodedImageResource("camera.png");
    EncodedImage sizeEnc = ImageResizer.sizeImage(enc, Display.getHeight(), Display.getHeight());

    takenPicture = new BitmapField(enc.getBitmap());

    VerticalFieldManager vfMain = new VerticalFieldManager();
    vfMain.add(new SeparatorField());
    vfMain.add(newImage);
    vfMain.add(chooseExisting);
    vfMain.add(takenPicture);
    add(vfMain);

    currentPicture = Storage.getPicture(currentProduct);
    showPicture();

}   

public void choosePicture(){

    String imageExtensions[] = {"jpg", "jpeg",
         "bmp", "png", "gif"};

    FileSelectorPopupScreen fps = new FileSelectorPopupScreen(null, imageExtensions);
    fps.pickFile();
    String theFile = fps.getFile();
    UiApplication.getUiApplication().pushScreen(fps);

    if (theFile == null)
    {
        Dialog.alert("Screen was dismissed. No file was selected.");
    }
    else
    {
        try{

            String path= "file:///" + theFile;
            byte[] data = getData(path);
            //Encode and Resize image 
            EncodedImage  eImage = EncodedImage.createEncodedImage(data,0,data.length);

            if(Display.getHeight()>Display.getWidth()){
                int scaleFactorX = Fixed32.div(Fixed32.toFP(eImage.getWidth()), 
                     Fixed32.toFP(Display.getWidth()));
                int scaleFactorY = Fixed32.div(Fixed32.toFP(eImage.getHeight()), 
                     Fixed32.toFP((Display.getWidth()*Display.getWidth())/Display.getHeight()));
                eImage=eImage.scaleImage32(scaleFactorX, scaleFactorY);
            }
            else{
                int scaleFactorX = Fixed32.div(Fixed32.toFP(eImage.getWidth()), 
                         Fixed32.toFP(Display.getWidth()));
                    int scaleFactorY = Fixed32.div(Fixed32.toFP(eImage.getHeight()), 
                         Fixed32.toFP(Display.getHeight()));
                    eImage=eImage.scaleImage32(scaleFactorX, scaleFactorY);
            }
            UiApplication.getUiApplication().popScreen(fps);
            takenPicture.setBitmap(eImage.getBitmap());

            Storage.setPicture(new Pics(currentProduct, path));

            try{
                Storage.savePicture();
            }catch(Exception e){
                e.printStackTrace();
            }

        }
        catch(Exception e){

        }       
        Dialog.alert("Picture Saved");
    }

}
public void showPicture(){
    if(currentPicture != ""){
        try{

            String path= currentPicture;
            byte[] data = getData(path);
            //Encode and Resize image 
            EncodedImage  eImage = EncodedImage.createEncodedImage(data,0,data.length);

            if(Display.getHeight()>Display.getWidth()){
                int scaleFactorX = Fixed32.div(Fixed32.toFP(eImage.getWidth()), 
                     Fixed32.toFP(Display.getWidth()));
                int scaleFactorY = Fixed32.div(Fixed32.toFP(eImage.getHeight()), 
                     Fixed32.toFP((Display.getWidth()*Display.getWidth())/Display.getHeight()));
                eImage=eImage.scaleImage32(scaleFactorX, scaleFactorY);
            }
            else{
                int scaleFactorX = Fixed32.div(Fixed32.toFP(eImage.getWidth()), 
                         Fixed32.toFP(Display.getWidth()));
                    int scaleFactorY = Fixed32.div(Fixed32.toFP(eImage.getHeight()), 
                         Fixed32.toFP(Display.getHeight()));
                    eImage=eImage.scaleImage32(scaleFactorX, scaleFactorY);
            }





            takenPicture.setBitmap(eImage.getBitmap());

        }
        catch(Exception e){

        }  
    }
}

public void fieldChanged(Field field, int context) {

    if(field == logo){

    }
    else if(field == newImage){
        takePicture();
    }
    else if(field == chooseExisting){
        choosePicture();
    }
}

}

I removed many parts of this class, sorry if it is long, there are basically 4 Storage method calls in this code and I believe they are all in the right place.. but again im having the problem on first load, not afterwards.

Can anyone see what Im doing wrong here? I have had this problem for a week

Thanks for any help provided!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:57:40+00:00Added an answer on June 4, 2026 at 6:57 am

    Try something like this:

    public class Storage extends Application{ 
    
        private static final long PERSISTENT_KEY8 = 0x2c4c45c139ee9728L; 
        static PersistentObject store8 = null; 
        private static Vector pics = null; 
    
        static{
            store8 = PersistentStore.getPersistentObject(PERSISTENT_KEY8)
            pics = (Vector)store8.getContents(); 
            if(pics == null){ 
                pics = new Vector(); 
                store8.setContents(pics);
                store8.commit();
            } 
        }
    
        public static void savePicture(){
            try{
                synchronized(store8){ 
                    store8.setContents(pics);  
                    store8.commit(); 
                }
            }
            catch(Exception e){       
                e.printStackTrace();       
            }       
        } 
    
        public static int findPicture(String productName){
            for(int i = 0; i < pics.size(); i++){ 
                Pics product = (Pics)pics.elementAt(i); 
                if(product.getProductName().equals(productName)){ 
                    return i; 
                } 
            } 
            return -1; 
        } 
    
        public static String getPicture(String productName){
            int idx = findPicture(productName);
            if(idx != -1){
                return ((Pics)pics.elementAt(idx)).getPic(); 
            } 
            return ""; 
        } 
    
        public static void removePicture(String productName){
            int idx = findPicture(productName);
            if(idx != -1){
                pics.removeElementAt(idx); 
            }    
        }    
    
        public static void setPicture(Pics pro){ 
            removePicture(pro.getProductName());
            pics.addElement(pro); 
        } 
    }
    

    .

    public class ProductImage extends MainScreen implements FieldChangeListener, AppLaunchResource {       
        private ImageButtonField logo;       
        private ButtonField newImage, chooseExisting;       
        public static BitmapField takenPicture;       
        //public static String picPath ="";        
        private String currentPicture = "";       
        private String currentProduct = "";       
    
        public ProductImage(String productName){       
            super(VERTICAL_SCROLL|VERTICAL_SCROLLBAR);       
            currentProduct = productName;       
            createGUI();        
        }       
    
        public void createGUI(){       
            deleteAll();       
            setTitle(new LabelField("Add An Image", Field.FIELD_HCENTER));       
            Toolbar tb = new Toolbar();       
            if(ToolbarManager.isToolbarSupported()){       
                setToolbar(tb.createToolBar());       
            }       
            else{       
                add(tb.createNavBar());       
            }       
    
            newImage = new ButtonField("Take Photo", ButtonField.CONSUME_CLICK){       
                public int getPreferredWidth() {       
                    return (int) Display.getWidth();       
                }       
            };       
    
            chooseExisting = new ButtonField("Change Image", ButtonField.CONSUME_CLICK){       
                public int getPreferredWidth() {       
                    return (int) Display.getWidth();       
                }       
            };       
    
            newImage.setChangeListener(this);       
            chooseExisting.setChangeListener(this);       
    
            EncodedImage enc = EncodedImage.getEncodedImageResource("camera.png");       
            EncodedImage sizeEnc = ImageResizer.sizeImage(enc, Display.getHeight(), Display.getHeight());       
    
            takenPicture = new BitmapField(enc.getBitmap());       
    
            VerticalFieldManager vfMain = new VerticalFieldManager();       
            vfMain.add(new SeparatorField());       
            vfMain.add(newImage);       
            vfMain.add(chooseExisting);       
            vfMain.add(takenPicture);       
            add(vfMain);       
    
            currentPicture = Storage.getPicture(currentProduct);       
            showPicture();       
        }          
    
        public void choosePicture(){       
            String imageExtensions[] = {"jpg", "jpeg", "bmp", "png", "gif"};       
    
            FileSelectorPopupScreen fps = new FileSelectorPopupScreen(null, imageExtensions);       
            fps.pickFile();       
            String theFile = fps.getFile();       
    
            if (theFile == null){       
                Dialog.alert("Screen was dismissed. No file was selected.");       
                return;
            }       
    
            EncodedImage eImage = loadImage("file:///" + theFile);
            if(eImage != null){
                takenPicture.setBitmap(eImage.getBitmap());       
                Storage.setPicture(new Pics(currentProduct, path));       
                Storage.savePicture();       
                Dialog.alert("Picture Saved");       
            }       
        }       
    
        private EncodedImage loadImage(String path){
            try{       
                byte[] data = getData(path);       
                //Encode and Resize image        
                EncodedImage  eImage = EncodedImage.createEncodedImage(data,0,data.length);       
    
                int scaleFactorX, scaleFactorY;
    
                if(Display.getHeight()>Display.getWidth()){       
                    scaleFactorX = Fixed32.div(Fixed32.toFP(eImage.getWidth()), Fixed32.toFP(Display.getWidth()));       
                    scaleFactorY = Fixed32.div(Fixed32.toFP(eImage.getHeight()), Fixed32.toFP((Display.getWidth()*Display.getWidth())/Display.getHeight()));       
                }       
                else{       
                    scaleFactorX = Fixed32.div(Fixed32.toFP(eImage.getWidth()), Fixed32.toFP(Display.getWidth()));       
                    scaleFactorY = Fixed32.div(Fixed32.toFP(eImage.getHeight()), Fixed32.toFP(Display.getHeight()));       
                }       
    
                return eImage.scaleImage32(scaleFactorX, scaleFactorY);
            }
            catch (Exception e){
                return null;
            }
        }
    
        public void showPicture(){       
            if(currentPicture != ""){       
                EncodedImage eImage = loadImage(currentPicture);
                if (eImage != null){
                    takenPicture.setBitmap(eImage.getBitmap());       
               }
            }       
        }       
    
        public void fieldChanged(Field field, int context){       
            if(field == logo){       
            }       
            else if(field == newImage){       
                takePicture();       
            }       
            else if(field == chooseExisting){       
                choosePicture();       
            }       
        }       
    }       
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to save image captured from web cam with current time.Like 06.06.2010
I am trying save an ImageButton's path in savedInstanceState in order not lose image
I am trying to save an uploaded image from an iPhone to a server
I'm trying to save a copied image from the clipboard but it's losing its
I'm trying to get an image from an URL but when I save it
I'm trying to do a simple thing; read an image from the internet, save
I'm trying to save an image like this (from an asp.net mvc application): public
I'm trying to write a method to store an image from a given url,
I am trying to save a record which has a many-to-one property mapping. I
I'm trying to save an excel spreadhseet from SQL Server Integration Services 2005. Unfortunatly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.