使用KitKat中提供的SAF,不会在保存到设备内部或外部存储点的文件上调用MediaScanner。因此,我必须根据返回的URI确定是否应该尝试运行MediaScanner。
// The SAF uses content URI to pass meta about the file. The following host is used for internal storage.
if (mExportServiceUri.getHost().equals("com.android.externalstorage.documents")) {
final File externalStorage = Environment.getExternalStorageDirectory();
final String path = mExportServiceUri.getEncodedPath().replace("/document/primary%3A", "");
MediaScannerConnection.scanFile(mService.getApplicationContext(), new String[] { new File(
externalStorage, path).getAbsolutePath() }, null, null);
}
有没有其他人必须解决这个问题,如果是这样,有没有比这更好的方法?目前,这仅支持设备外部存储,并且需要在单独的检查中处理诸如SDCard之类的额外存储空间。
答案 0 :(得分:0)
支持我认为所有可能的安装,包括通过OTG连接的USB拇指驱动器,甚至可能直接连接到某些平板电脑上的全尺寸USB端口(我没有平板电脑来测试,有4.4平板电脑甚至存在有一个全尺寸端口?)我有以下似乎在Galaxy S4(Play商店版)和N5上运行良好。
// The SAF uses content URI to pass meta about the file. The following host is used for SD storage.
if (mExportServiceUri.getHost().equals("com.android.externalstorage.documents")) {
final String encodedPath = mExportServiceUri.getEncodedPath();
final String path = encodedPath.substring(encodedPath.indexOf("%3A") + 3);
final File[] storagePoints = new File("/storage").listFiles();
// document/primary is in /storage/emulated/legacy and thus will fail the exists check in the else handling loop check
if (encodedPath.startsWith("/document/primary")) {
// External file stored in Environment path
final File externalFile = new File(Environment.getExternalStorageDirectory(), path);
MediaScannerConnection.scanFile(mService.getApplicationContext(),
new String[] { externalFile.getAbsolutePath() }, null, null);
} else {
// External file stored in one of the mount points, check each mount point for the file
for (int i = 0, j = storagePoints.length; i < j; ++i) {
final File externalFile = new File(storagePoints[i], path);
if (externalFile.exists()) {
MediaScannerConnection.scanFile(mService.getApplicationContext(),
new String[] { externalFile.getAbsolutePath() }, null, null);
break;
}
}
}
}