2017-06-16 47 views
1

我試圖將圖像保存到iOS中的存儲時遇到問題。圖像已下載但未保存。iOS保存到存儲問題

的代碼是:

 Form hi = new Form("Toolbar", new BoxLayout(BoxLayout.Y_AXIS)); 
    TreeModel tm = new TreeModel() { 
     @Override 
     public Vector getChildren(Object parent) { 
      String[] files; 
      if (parent == null) { 
       files = FileSystemStorage.getInstance().getRoots(); 
       return new Vector<Object>(Arrays.asList(files)); 
      } else { 
       try { 
        files = FileSystemStorage.getInstance().listFiles((String) parent); 
       } catch (IOException err) { 
        Log.e(err); 
        files = new String[0]; 
       } 
      } 
      String p = (String) parent; 
      Vector result = new Vector(); 
      for (String s : files) { 
       result.add(p + s); 
      } 
      return result; 
     } 

     @Override 
     public boolean isLeaf(Object node) { 
      return !FileSystemStorage.getInstance().isDirectory((String) node); 
     } 
    }; 
    Command tree = new Command("Show tree") { 
     @Override 
     public void actionPerformed(ActionEvent evt) { 
      Form treeForm = new Form("Tree", new BorderLayout()); 
      Tree t = new Tree(tm) { 
       @Override 
       protected String childToDisplayLabel(Object child) { 
        String n = (String) child; 
        int pos = n.lastIndexOf("/"); 
        if (pos < 0) { 
         return n; 
        } 
        return n.substring(pos); 
       } 
      }; 
      treeForm.add(BorderLayout.CENTER, t); 
      Command back = new Command("Back") { 
       @Override 
       public void actionPerformed(ActionEvent evt) { 
        hi.showBack(); 
       } 
      }; 
      Button backButton = new Button(back); 
      treeForm.add(BorderLayout.SOUTH, backButton); 
      treeForm.show(); 
     } 

    }; 
    hi.getToolbar().addCommandToOverflowMenu(tree); 
    EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(hi.getWidth(), hi.getWidth()/5, 0xffff0000), true); 
    String photoURL = "https://awoiaf.westeros.org/images/thumb/9/93/AGameOfThrones.jpg/400px-AGameOfThrones.jpg"; 
    StringBuilder fsPath = new StringBuilder(FileSystemStorage.getInstance().getAppHomePath()); 
    fsPath.append("400px-AGameOfThrones.jpg"); 
    URLImage background = URLImage.createToStorage(placeholder, fsPath.toString(), photoURL); 
    background.fetch(); 
    Style stitle = hi.getToolbar().getTitleComponent().getUnselectedStyle(); 
    stitle.setBgImage(background); 
    stitle.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL); 
    stitle.setPaddingUnit(Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS); 
    stitle.setPaddingTop(15); 
    SpanButton credit = new SpanButton("Link"); 
    credit.addActionListener((e) -> Display.getInstance().execute("https://awoiaf.westeros.org/index.php/A_Game_of_Thrones")); 
    hi.add(new SpanLabel("A")). 
      add(new Label("B", "Heading")). 
      add(credit); 

    ComponentAnimation title = hi.getToolbar().getTitleComponent().createStyleAnimation("Title", 200); 
    hi.getAnimationManager().onTitleScrollAnimation(title); 
    hi.show(); 

這是一個從https://www.codenameone.com/javadoc/com/codename1/ui/URLImage.html

樹採取是隻看到在拍攝時保存在存儲。

回答

0

你在混合Storage & FileSystemStorage這是非常不同的東西看this

可以使用存儲這是一個平坦的一套「文件」,這就是URLImage.createToStorage一樣。但是,您需要使用Storage API來處理該問題,並且它可能在FileSystemStorage API中不可見。您可能還會喜歡URLImage.createToFileSystem()