如何使用Codenameone从存储复制到FileSystemStorage并在BrowserComponent中显示

时间:2018-11-01 01:21:20

标签: ios codenameone

我已经阅读了很多StackOverflow帖子,其中讨论了将数据从FileSystemStorage复制到CodenameOne中的存储,例如Shai的this answer中所述,如下所示:

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

我一直在尝试相反的操作:从存储保存到FileSystemStorage,以便在BrowserComponent中显示PDF(使用iOS时),但无法这样做。我需要在应用程序内显示 PDF (所以我不想使用Display.getInstance().execute())。

基本上,我正在尝试用用户选择的任何文件来动态填充Container-我正在使用FileChooser library for CN1 from Steve Hannah。 (免责声明:我对正在使用的应用中使用的该库进行了一些修改-但是,当我选择该库中的图像并通过InputStream将它们从存储拉到图像时,它们可以在ImageViewer中完美显示因此我知道所有文件都已正确保存在存储设备中。)

这是我的代码(在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);
        }

ToastBar为truetrue显示file.exists()file.isFile()

我之所以规定iOS,是因为据我在研究在应用内预览PDF时所看到的,我已经看到Android需要采用不同的实现,例如在Android库中添加NativeInterface 。我还在Google网上论坛的不同答案中看到,此功能(使用browserComponent查看PDF)仅适用于iOS,不适用于模拟器。在模拟器中,我看到一个空白。显示ToastBar之后,我的iPhone只是冻结和/或崩溃(而且我在Windows计算机上工作,因此查看本机日志的功能不多...。)

我该怎么做才能访问文件并将其显示在BrowserComponent中?

谢谢!

1 个答案:

答案 0 :(得分:1)

简单的解决方案-文件中有空格(例如“ Test page.pdf”),并且没有显示!当我使用没有空格的文件时,可以正常工作,并且在删除文件名中的空格后,值得庆幸的是,一切正常。我将不得不添加代码来处理这种情况。

感谢您的帮助!