正在 android 中保存 public 个文件,但 PC 无法访问文件夹
Saving public files in android but folders are not accessible by PC
我正在尝试为我的应用程序创建一些 public 文件夹以保存一些数据。
我的目标是创建一个主文件夹和一个根据当前日期命名的子文件夹,以保存我的数据。数据应该可以通过 PC 访问。
问题是当我通过 phone[图 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) {
}
});
我没有包含我的其他函数,因为它们不负责创建路径和文件。
我希望你能帮助我。
谢谢
扫描 myfile
,而不是 root
,并且只有在您写入 并关闭 您的 FileWriter
.
之后
我找到了解决办法。我将代码更改为
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 答案中的部分也是必需的。
我正在尝试为我的应用程序创建一些 public 文件夹以保存一些数据。 我的目标是创建一个主文件夹和一个根据当前日期命名的子文件夹,以保存我的数据。数据应该可以通过 PC 访问。 问题是当我通过 phone[图 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) {
}
});
我没有包含我的其他函数,因为它们不负责创建路径和文件。 我希望你能帮助我。 谢谢
扫描 myfile
,而不是 root
,并且只有在您写入 并关闭 您的 FileWriter
.
我找到了解决办法。我将代码更改为
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 答案中的部分也是必需的。