user3025401
user3025401

Reputation: 189

Saving and retrieving files in Codenameone

I have an app with data files (some images and xml files) i have packed them up in a zip file.

I open the file with zipme and save the files. I used this code for that

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

i see in netbeans that in the simulator the files are saved to users/.cn1 So this works on the desktop

To fetch the image i use

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

i also tried without outdir but also no luck. What do i wrong.

Upvotes: 1

Views: 105

Answers (2)

user3025401
user3025401

Reputation: 189

The answer i found on my question is: 1 No extra slash as Shai Among suggested: 2 Make a inputstream for enecodedimage.create() instead of only a string with the path to the file

Without the second part the app doesn't run correctly in the simulation and on the device

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

Upvotes: 0

Shai Almog
Shai Almog

Reputation: 52770

This should work without the extra slash:

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

Notice that this code is case sensitive so make sure the file has the right casing. Is this failing on the simulator, if so place a breakpoint on the loading code and make sure the file is physically there

Upvotes: 0

Related Questions