0

ユーザーが履歴リストから特定の製品を選択したときに画像を表示するために、永続ストアに画像へのパスを保存しようとしています (ユーザーが撮影した製品ごとに 1 つの画像)。今私が抱えている問題は、初めてアプリを電話にロードして永続ストアにエントリを追加しようとすると、少し後にエラーがスローされ、アプリがフリーズすることです。ただし、戻って画像を再度追加すると、問題なく機能し、特定の製品を選択すると常に画像が読み込まれます。

これにより、原因は私が実行している最初の store8.commit() であると信じるようになりました。何らかの理由で、デバッグ時に例外がスローされます: No Stack Trace。これが私のコードです:

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);
}
}

これが、画像を保存するためのメソッドを含むクラスです。今私がそれを操作している他のクラスのために:

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();
    }
}

}

このクラスの多くの部分を削除しました。長い場合は申し訳ありません。このコードには基本的に 4 つのストレージ メソッド呼び出しがあり、それらはすべて適切な場所にあると思います。

ここで私が間違っていることを誰でも見ることができますか? 私は一週間この問題を抱えていました

助けてくれてありがとう!

4

1 に答える 1

1

次のようなことを試してください:

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();       
        }       
    }       
}       
于 2012-05-22T17:22:42.547 に答える