首页 文章

如何使用Codename One和CN1 Filechooser在本地保存并检索文件?

提问于
浏览
1

我已经在Codename One网站上研究了Files, Storage and Networking指令 . 我在尝试着

  • save 由Codename One filechooser选择的文件到设备(模拟器,Android,iPhone)

  • retrieve 本地保存在保存的任何设备中的文件 .

这是我使用的代码:

final Button getFileChooser = new Button("Get file chooser");
    getFileChooser.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {

            if (FileChooser.isAvailable()) {
                FileChooser.showOpenDialog(".pdf, .jpg, .jpeg, .png", e2-> {
                    String file = (String)e2.getSource();
                    if (file == null) {
                        hi.add("No file was selected");
                        hi.revalidate();
                    } else {
                        String extension = null;
                        if (file.lastIndexOf(".") > 0) {
                            extension = file.substring(file.lastIndexOf(".")+1);
                            ToastBar.showMessage("1: " + file, FontImage.MATERIAL_CHECK);
                            rawFilename.setText("raw: " + file);
                            //java.util.List<String> filepathParsed = StringUtil.tokenize(file, "[/.]");
                            StringBuilder hi = new StringBuilder(file);
                            if (file.startsWith("file://"))
                                hi.delete(0, 7);
                            int lastIndexPeriod = hi.toString().lastIndexOf(".");
                            Log.p(hi.toString());
                            String ext = hi.toString().substring(lastIndexPeriod);
                            String hmore = hi.toString().substring(0, lastIndexPeriod -1);

                            String hi2 = hmore +/* "/file" +*/ ext;/*hi;*/
                            Log.p("hi2 = " + hi2);
                            secondFilename.setText("2nd: " + hi2);
                            //secondFilename.setText((String)evt.getSource());
                            try {
                                saveFileToDevice(file, ext);
                            } catch (URISyntaxException e) {
                                e.getMessage();
                            }
                            //sendFileViaEmail(hi2, file);


                        } else {
                            ToastBar.showMessage("2: " + file + " " + extension, FontImage.MATERIAL_CHECK);
                        }
                    }
                    hi.revalidate();
                });
            }
        }
    });

 protected void saveFileToDevice(String hi, String ext) throws URISyntaxException{
    URI uri = new URI(hi);
    String path = uri.getPath();
    Log.p("uri path: " + uri.getPath() + " ; scheme: " + uri.getScheme());
    /*if (path.contains("..jpg")) {
        int indexLastPeriodBeforeExt = path.lastIndexOf(".");
        path = path.substring(0, indexLastPeriodBeforeExt) + path.substring(indexLastPeriodBeforeExt+ 1);
        Log.p("new path: " + path);

    }*/
    if (Display.getInstance().getPlatformName().equals("and"))
        path = "file://" +path;
    String home = FileSystemStorage.getInstance().getAppHomePath();
    char sep = FileSystemStorage.getInstance().getFileSystemSeparator();
    String userDir = home + sep + "myapp";
    /*if (hi.startsWith("file://")) {
        hi = hi.substring(7);
    }*/
    int index = hi.lastIndexOf("/");
    hi = hi.substring(index + 1);
    Log.p("hi after substring 7: " + hi);
    FileSystemStorage.getInstance().mkdir(userDir);
    String fPath = userDir + sep + hi ;
  /*  if (hi.contains("..jpg")) {
        int indexLastPeriodBeforeExt = hi.lastIndexOf(".");
        hi = hi.substring(0, indexLastPeriodBeforeExt) + hi.substring(indexLastPeriodBeforeExt+ 1);
        Log.p("new hi: " + hi);
    }*/

        Log.p("does it exist? " + FileSystemStorage.getInstance().exists(userDir));
        String imageFile = userDir + sep + hi;
         String[] roots = FileSystemStorage.getInstance().getRoots();
            if (roots.length > 0) {
                for (int i = 0; i < roots.length; i++)
                    Log.p("roots" + i + " " + roots[i]);
            }
            try {
                String[] files = FileSystemStorage.getInstance().listFiles(userDir);
                for (int i = 0; i < files.length; i++) {
                    Log.p(i + " " + files[i]);
                    //secondName = files[files.length-1];
                    Log.p("secondname: " + secondName);
                }
                sendFileViaEmail(imageFile, ".jpg");
            } catch (IOException e) {
                e.getMessage();
            }
        }

protected void sendFileViaEmail(String hi2, String file) {
    Message m = new Message("Test message");
    //m.getAttachments().put(file, "image/jpeg");
    m.setAttachment(hi2);
    m.setAttachmentMimeType(Message.MIME_IMAGE_JPG);
    //m.setMimeType(Message.MIME_IMAGE_JPG);
    //m.getAttachments().put(imageAttachmentUri, "image/png");
    Display.getInstance().sendMessage(new String[] {"myemail@example.com"}, "test message cn1", m);
}

在将cn1-filechooser library导入我的项目并刷新了库之后,我将 getFileChooser 按钮添加到了我的表单中 . filechooser工作 - 我在模拟器,Android设备和iPhone上试过它 .

我的问题在这里:

String file 检索看起来像URI的内容 . 我试图将URI转换为文件,当我运行以下代码以查看文件存储时,我在我的日志中看到它们,尽管有趣的顺序,我很好 .

String[] files = FileSystemStorage.getInstance().listFiles(userDir);
            for (int i = 0; i < files.length; i++) {
                Log.p(i + " " + files[i]);
                //secondName = files[files.length-1];
                Log.p("secondname: " + secondName);
            }

How do retrieve a valid, uncorrupted version of this file from FileSystemStorage? Am I storing it wrong? 每当我运行该方法通过电子邮件发送文件时,它会打开Outlook(在我的桌面上),但没有附加文件? (我假设文件在尝试从 FileSytemStorage 检索时无效/损坏 - 请帮助 . )我将 .jpg 硬编码为MIME类型,但仅作为测试;我的目标是能够保存/恢复.pdf,.png,.jpg和.jpeg文件 .

谢谢!

**编辑:**这似乎表明该文件实际上已保存到我的应用程序的存储中 . 如何从我的应用程序中复制/上传或通过电子邮件传输文件内容?

1 回答

  • 0

    手机不允许这样 . 他们没有像台式机那样的文件系统 . 因此,文件选择器确实启动了一个工具来帮助您从不同的应用程序中选择一个文件,然后将此文件复制到您的本地应用程序沙箱,您可以在其中访问该应用程序的内容 .

    当然,这对于保存不起作用,因为这意味着您可能会损坏另一个应用程序中的文件,因此您无法写入已安装的其他应用程序的目录,甚至知道安装了哪些应用程序以及...

    要将文件传输到其他应用程序,您可以通过共享按钮或显示的低级API来使用共享意图 . 这允许您将图像发送到whatsapp或在移动电话中有意义的类似功能 .

相关问题