【问题标题】:Saving public files in android but folders are not accessible by PC在android中保存公共文件但PC无法访问文件夹
【发布时间】:2017-07-10 18:56:23
【问题描述】:

我正在尝试为我的应用创建一些公用文件夹以保存一些数据。 我的目标是创建一个主文件夹和一个根据当前日期命名的子文件夹,以保存我的数据。数据应该可以通过 PC 访问。 问题是当我通过手机访问文件时一切正常[图 1](使用蓝牙文件传输应用程序),电脑无法识别子文件夹[图 2]。

[图 1] 全部在移动设备上运行

[图 2] PC 上无法识别子文件夹

我查看了很多答案,尝试了很多但没有找到解决方案。 我的代码是:

public void createFile() {
    calendar_time = Calendar.getInstance(Locale.getDefault());
    int hour = calendar_time.get(Calendar.HOUR_OF_DAY);
    int minute = calendar_time.get(Calendar.MINUTE);
    int second = calendar_time.get(Calendar.SECOND);
    String time=String.valueOf(hour)+":"+String.valueOf(minute)+":"+String.valueOf(second);
    String sFileName=date+" "+time+" Part "+String.valueOf(file_counter)+".txt";

    try {
        File root = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS), "Main folder/"+date);
        if (!root.exists()) {
            root.mkdirs();
        }

        root.setExecutable(true);
        root.setReadable(true);
        root.setWritable(true);
        MediaScannerConnection.scanFile(context, new String[] { root.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                    }
                });

        File myfile = new File(root, sFileName);
        writer = new FileWriter(myfile);
        writer.append("Unit\n\n");
        writer.append("#\tValue \tX \tY \tZ \tTime\n\n");
        readyToRecord=true;
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, "Error creating file", Toast.LENGTH_SHORT).show();
        stop();
    }
}

我添加了以下代码,因为我阅读了它会有所帮助

root.setExecutable(true);
        root.setReadable(true);
        root.setWritable(true);
        MediaScannerConnection.scanFile(context, new String[] { root.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                    }
                });

我没有包含我的其他函数,因为它们不负责创建路径和文件。 我希望你能帮助我。 谢谢

【问题讨论】:

    标签: android file directory android-external-storage android-mediascanner


    【解决方案1】:

    我找到了解决方案。我将代码更改为

            File root = new File(Environment.getExternalStorageDirectory()+ "/Main folder/"+date);
            if (!root.exists()) {
                root.mkdirs();
            }
            myfile = new File(root.getAbsolutePath(), sFileName);
            writer = new FileWriter(myfile);
    

    与 getExternalStoragePublicDirectory( Environment.DIRECTORY_DOCUMENTS)

    CommonsWare 回答中的部分也是必要的。

    【讨论】:

      【解决方案2】:

      扫描myfile,而不是root,只有在你写信并关闭你的FileWriter之后。

      【讨论】:

      • 不幸的是它没有任何区别。
      • 我会在不同的设备上尝试
      • 我用你的建议和一些不同的方法修复了它。谢谢
      猜你喜欢
      • 2020-02-27
      • 2015-07-08
      • 2011-01-30
      • 2021-02-10
      • 2022-08-14
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      相关资源
      最近更新 更多