1

I've been reading a lot of StackOverflow posts that discuss copying data from FileSystemStorage to Storage in CodenameOne, such as described in this answer from Shai, as seen below:

InputStream stream =
FileSystemStorage.getInstance().openInputStream(i); 
OutputStream out =
Storage.getInstance().createOutputStream("MyImage"); 
Util.copy(stream, out); 
Util.cleanup(stream); 
Util.cleanup(out);`

I've been trying to do the reverse: save from Storage to FileSystemStorage in order to show a PDF in the BrowserComponent (while using iOS), but have not been able to do so. I need to show the PDF within the app (so I don't want to use Display.getInstance().execute()).

Basically, I'm trying to dynamically populate a Container with whatever files the user selects-- I am using the FileChooser library for CN1 from Steve Hannah. (Disclaimer: I have made slight modifications to this library as it used in the app I'm working on-- HOWEVER, when I choose images with this library and pull them from Storage to an Image via InputStream, they display perfectly in an ImageViewer so I know that all files are being saved correctly in Storage.)

Here is my code (with help from Steve Hannah's comment on GitHub):

        //fileLocation and fileName are slightly different but both end with file extension
        File file = new File(fileToUpload.getFileName());
        FileSystemStorage fss = FileSystemStorage.getInstance();
        InputStream is = Storage.getInstance().createInputStream(fileToUpload.getLocation());
        OutputStream os = fss.openOutputStream(file.getAbsolutePath());
        Util.copy(is, os);
        ToastBar.Status status = ToastBar.getInstance().createStatus();
        String message = file.exists() + "  " + file.isFile() + file.getAbsolutePath();
        status.setMessage(message);
        status.setExpires(3000);
        status.show();
        NativeLogs.getNativeLogs();
        if (Display.getInstance().getPlatformName().equals("ios")) {
            //Log.p("in ios !!!!");
            BrowserComponent browserComponent = new BrowserComponent();
            browserComponent.setURL(file.getPath());
            horizontalContainer.add(browserComponent);
        }

The ToastBar displays true and true for file.exists() and file.isFile().

I stipulate iOS because as far as I've seen while researching previewing PDFs within an app, I've seen that Android needs to have a different implementation, like adding a NativeInterface with an Android library. I also saw in different answers on the Google Group that this functionality (using browserComponent to view PDFs) is only available for iOS and not on the simulator. In the simulator, I see a blank space. My iPhone just freezes and/or crashes after displaying the ToastBar (and I work on a Windows machine, so not much ability to see native logs....)

What can I do to access the file and show it in the BrowserComponent?

Thank you!

app-dev
  • 348
  • 1
  • 2
  • 12
  • Just to check if this is the case (don't leave it for the final app) try adding the build hint `ios.plistInject=NSAppTransportSecurityNSAllowsArbitraryLoads` and see if it works around the issue – Shai Almog Nov 01 '18 at 03:19
  • Thanks. I am already using that build hint in my app and am experiencing this problem. If I take out this build hint, I am unable perform login protocols on the app on my iPhone. Anything else I can try? As far as I can tell, the data is copied into FileSystemStorage, but I can't display it. Do you think I need a different build hint in order to allow the BrowserComponent to display the PDF? – app-dev Nov 01 '18 at 03:34

1 Answers1

1

Simple solution -- the file had a space in it (eg. "Test page.pdf") and didn't show! When I used files that didn't have spaces this worked and after removing spaces in the file names, thankfully everything worked. I'll have to add code to handle this scenario.

Thanks for your help!

app-dev
  • 348
  • 1
  • 2
  • 12