在Android中保存公共文件,但PC无法访问文件夹 [英] Saving public files in android but folders are not accessible by PC

查看:150
本文介绍了在Android中保存公共文件,但PC无法访问文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用创建一些公用文件夹以保存一些数据。
我的目标是创建一个基于当前日期命名的主文件夹和一个子文件夹,以便保存我的数据。数据应该可以通过PC访问。
问题是,当我通过电话访问文件[图像1](使用Bluetooth File Transfer应用程序)时,一切正常,但PC无法识别子文件夹[图像2]。

I am trying to create some public folders for my app to save some data. My goal is to create a main folder and a subfolder named based on the current date in order to save my data. The data are supposed to be accessible via PC. The problem is the while everything is working when I access the files via the phone[Image 1] (with Bluetooth File Transfer app) the pc fails to recognize the subfolder[Image 2].

[图片1]所有在移动
上工作

[Image 1] All working on mobile

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

[Image 2] Subfolder not recognized on PC

我看过很多答案,尝试了很多但都没有找到一个解法。
我的代码是:

I have looked at many answers, tried a lot but haven't found a solution. My code is:

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();
    }
}

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

I added the following code because I read it would help

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) {
                    }
                });

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

I don't include my other functions because they are not responsible for creating the path and file. I hope you can help me. Thank you

推荐答案

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

Scan myfile, not root, and only after you have written to and closed your FileWriter.

这篇关于在Android中保存公共文件,但PC无法访问文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆