[英]Rescan older files with mediascannerconnection in local storage. files get not updated on windows
My android-application writes a bunch of csv-files and jpg-files to the internal storage of the device.我的 android 应用程序将一堆 csv 文件和 jpg 文件写入设备的内部存储。 I am using
MediaScannerConnection.scanFile()
to make the files accessable from my windows-system without rebooting the android-device.我正在使用
MediaScannerConnection.scanFile()
使文件可以从我的 Windows 系统访问,而无需重新启动 android 设备。
private void scanFiles() {
File targetDirectory = new File(Environment.getExternalStorageDirectory(), "DIR_OF_MY_APP");
if (targetDirectory.exists()) {
List<File> filesToScan = getFiles(targetDirectory);
List<String> filePathsToScan = new ArrayList<>();
for(File file : filesToScan) {
filePathsToScan.add(file.getPath());
}
MediaScannerConnection.scanFile(this, filePathsToScan.toArray(new String[0]), null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Log.d("OK", "Path: " + path);
Log.d("OK", "Uri : " + uri);
}
});
}
}
In my Logcat i can see every file is getting scanned.在我的 Logcat 中,我可以看到每个文件都在被扫描。 The new ones and the old ones.
新的和旧的。
My problem is when my app is adding new lines to an existing csv-file and the file is getting scanned, The new lines do not appear in the csv-file when its opend from my pc.我的问题是当我的应用程序向现有的 csv 文件添加新行并且文件被扫描时,当从我的电脑打开时,新行没有出现在 csv 文件中。 How can i fix this problem?
我该如何解决这个问题?
I already tried to rename all the files from filename
to tmp_filename
, rescann all the files and rename them back from tmp_filename
to filename
and rescann them again.我已经尝试将所有文件从
filename
重命名为tmp_filename
,重新扫描所有文件并将它们从tmp_filename
重命名为filename
并再次重新扫描它们。 After this, i have can see the oldfilename-file
and the tmp_oldfilename-file
on my windows-computer.在此之后,我可以在我的 Windows 计算机上看到
oldfilename-file
和tmp_oldfilename-file
。 The tmp_oldfilename-file
can not be opend (Unknown error on [memory-adress]).无法
tmp_oldfilename-file
([内存地址] 上的未知错误)。 The oldfilename-file
shows the not updated csv-file. oldfilename-file
显示未更新的 csv 文件。
I also tried to use a intent to scan the files, since some questions on so say its going to update them:我还尝试使用意图来扫描文件,因为一些问题说它会更新它们:
for(File file : filesToScan) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
Log.d("OK", "File: " + file.getName() + " scanned...");
}
here i can see the files getting scanned too, but they do not show up updated on my windows-computer.在这里我也可以看到文件被扫描,但它们没有显示在我的 Windows 计算机上。
Okay the only solution i came up with, is to set the usb-mode
to load-only
(this must be done by hand from the user) before performing the MediaScannerConnection.scanFile();
好吧,我想出的唯一解决方案是在执行
MediaScannerConnection.scanFile();
之前将usb-mode
设置为load-only
(这必须由用户手动完成MediaScannerConnection.scanFile();
. . After this is done, the user can set the
usb-mode
back to mtp
and than the csv-files will show up with the new added lines.完成后,用户可以将
usb-mode
设置回mtp
,然后 csv 文件将显示新添加的行。
This is a really bad workarround, but still better than rebooting the device.这是一个非常糟糕的解决方法,但仍然比重新启动设备好。 If someone has an better solution, pls share.
如果有人有更好的解决方案,请分享。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.