我现在找到了解决方案。我正在使用以下方法从 DocumentFile 中获取文件路径。将此路径发送到媒体扫描仪时,文件被正确扫描:
private static Object[] volumes;
public static Uri getDocumentFileRealPath(Context context, DocumentFile documentFile) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
final String docId = DocumentsContract.getDocumentId(documentFile.getUri());
final String[] split = docId.split(":");
final String type = split[0];
if (type.equalsIgnoreCase("primary")) {
File file = new File (Environment.getExternalStorageDirectory(), split[1]);
return Uri.fromFile(file);
} else {
if (volumes == null) {
StorageManager sm=(StorageManager)context.getSystemService(Context.STORAGE_SERVICE);
Method getVolumeListMethod = sm.getClass().getMethod("getVolumeList", new Class[0]);
volumes = (Object[])getVolumeListMethod.invoke(sm);
}
for (Object volume : volumes) {
Method getUuidMethod = volume.getClass().getMethod("getUuid", new Class[0]);
String uuid = (String)getUuidMethod.invoke(volume);
if (uuid != null && uuid.equalsIgnoreCase(type))
{
Method getPathMethod = volume.getClass().getMethod("getPath", new Class[0]);
String path = (String)getPathMethod.invoke(volume);
File file = new File (path, split[1]);
return Uri.fromFile(file);
}
}
}
return null;
}