我正在尝试在持久存储中保存图像的路径,以在用户从历史列表中选择特定产品时显示图像(每个产品一个图像,由用户拍摄)。现在我遇到的问题是,当我第一次将应用程序加载到手机并尝试向持久性存储添加一个条目时,它会在稍后引发错误并且应用程序冻结。但是,当我回来并再次添加图像时,它工作得很好,并且当我选择每个特定产品时,图像总是会加载。
这让我相信罪魁祸首是我正在做的第一个 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 个存储方法调用,我相信它们都在正确的位置。但我在第一次加载时再次遇到问题,而不是之后。
谁能看到我在这里做错了什么?我有这个问题一个星期
感谢您提供的任何帮助!