【问题标题】:Saving and retrieving files in Codenameone在 Codenameone 中保存和检索文件
【发布时间】:2021-07-13 00:04:41
【问题描述】:

我有一个包含数据文件(一些图像和 xml 文件)的应用程序,我已将它们打包在一个 zip 文件中。

我用 zipme 打开文件并保存文件。我为此使用了此代码

private void save1( ) {
      InputStream is;
         FileChooser.showOpenDialog(".zip", new ActionListener() {
                 @Override
            public void actionPerformed(ActionEvent e) {
                  if (e != null && e.getSource() != null) {
                    String file = (String)e.getSource();
                    FileSystemStorage fs = FileSystemStorage.getInstance();
                  try {
                      InputStream is  = fs.openInputStream(file);
                      ZipInputStream zipStream = new ZipInputStream(is);
                      ZipEntry entry;

                       // create a buffer to improve copy performance later.
                       byte[] buffer = new byte[2048];
                        while ((entry = zipStream.getNextEntry()) != null) {
                          String s = entry.getName();
                          String outdir = FileSystemStorage.getInstance().getAppHomePath();
                          if (outdir.length() > 0) {
                              outdir = outdir  ;
                          }
                          String outpath = outdir   + "/" +  entry.getName();
                          OutputStream output = null;
                          try {
                              output = FileSystemStorage.getInstance().openOutputStream(outpath);
                              int len = 0;
                              while ((len = zipStream.read(buffer)) > 0) {
                                  output.write(buffer, 0, len);
                             }

                          } finally {
                              // we must always close the output file
                              if (output != null) {
                                  output.close();
                              }
                          }
                        } } catch (IOException ex) {
  Log.p(ex.getMessage(), 0); }                 }      }});}

我在 netbeans 中看到模拟器中的文件保存到 用户/.cn1 所以这适用于桌面

获取我使用的图像

String outdir = FileSystemStorage.getInstance().getAppHomePath(); 图片 uur1 = EncodedImage.create(outdir + "/West.jpg");

我也试过没有 outdir,但也没有运气。 我哪里错了。

【问题讨论】:

  • 这应该可以在没有额外斜杠的情况下工作:Image uur1 = EncodedImage.create(outdir + "West.jpg");。请注意,此代码区分大小写,因此请确保文件具有正确的大小写。这是否在模拟器上失败,如果是这样,请在加载代码上放置一个断点并确保文件在物理上存在。
  • Thanx Shai ,我解决了我的问题 额外的斜线是个问题。我还注意到我必须创建一个输入流,而不是放入编码图像。创建(输入流)只有位置字符串不起作用另一个问题,当我将文件放入文件系统存储时。同名文件会被覆盖吗?
  • 我不懂这些问题。你能用一个样本详细说明吗?理想情况下提出一个不同的问题。我将在此处添加一个答案,以便您接受并获得积分。

标签: codenameone


【解决方案1】:

这应该可以在没有额外的斜杠的情况下工作:

Image uur1 = EncodedImage.create(outdir + "West.jpg");. 

请注意,此代码区分大小写,因此请确保文件的大小写正确。这在模拟器上是否失败,如果是这样,请在加载代码上放置一个断点并确保文件物理存在

【讨论】:

    【解决方案2】:

    我在我的问题上找到的答案是: 1 没有额外的斜线,如 Shai 建议的那样: 2 为 enecodedimage.create() 创建一个输入流,而不仅仅是一个带有文件路径的字符串

    如果没有第二部分,应用程序将无法在模拟和设备上正确运行

    FileSystemStorage fs = FileSystemStorage.getInstance();       
    String outdir = FileSystemStorage.getInstance().getAppHomePath();
    String outpath = outdir + West.jpg;
    InputStream isk   = fs.openInputStream(outpath);   
    Image uur = EncodedImage.create(isk);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多