能够接收文件但文件未存储在Android外部存储中

时间:2015-07-27 16:05:01

标签: android smack

我正在使用SMACK api和Spark进行测试来创建Xmpp应用程序。 我可以从Spark发送,但我看不到在Android库中创建的任何目录和文件。

enter image description here

我记录方法incoming.getAmountWritten(),它正在给我

07-27 21:00:58.789: V/Receiving Status ...(17652): 208861

这意味着我已经获得了该文件,现在我必须在物理存储上写。可能是我的Android代码有问题。

请找 Android代码

@Override
        public void fileTransferRequest(final FileTransferRequest fileRequest) {
            final File dir = Environment.getExternalStorageDirectory();
            final File folder = new File(dir+ "/illuxplain/");
            boolean success = true;
            if (!folder.exists()) {
                success = folder.mkdir();
            }

            if (success) {
                Thread receiving = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        IncomingFileTransfer incoming = fileRequest.accept();
                        Log.v("Receiving File Name", incoming.getFileName());
                        File file = new File(folder, incoming.getFileName());
                        try {
                            incoming.recieveFile(file);
                            while (!incoming.isDone()) {
                                try {
                                    Thread.sleep(1000L);
                                } catch (Exception e) {
                                    Log.e("", e.getMessage());
                                }
                                if (incoming.getStatus().equals(Status.error)) {
                                    Log.e("ERROR!!! ", incoming.getError() + "");
                                }
                                if (incoming.getException() != null) {
                                    incoming.getException().printStackTrace();
                                }
                            }

                            Log.v("Receiving Status ... ",""+incoming.getAmountWritten());
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.e("", e.getMessage());
                        }
                    }
                });

                receiving.start();
            }else{
                System.out.println("Directory Not Created");
            }
        }

    }

当我去画廊看看我是否收到了档案。我看到没有创建目录,当然也没有文件。

1 个答案:

答案 0 :(得分:-1)

您需要将数据写入文件。下面是一个将数据写入文件

的代码示例
    File file = new File(folder, incoming.getFileName());   


    String string = "Hello world!";
    FileOutputStream outputStream;

    try {
      outputStream = new FileOutputStream(file);
      outputStream.write(string.getBytes());
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }