目录
  1. 1. FileProvider 是什么
FileProvider 学习

FileProvider 是什么

FileProvider 是 ConetentProvider 的子类,在安卓 5.1 中添加到库中,这个是用来作为文件共享的组件。

我们通常在在 Android 7.0 之后通过getContentProvider.query根据 Uri 来查询文件信息。

可以看到下面代码,在查询的时候并没有给出_data的值,所以拿不到 file 的绝对路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
// ContentProvider has already checked granted permissions
final File file = mStrategy.getFileForUri(uri);

if (projection == null) {
projection = COLUMNS;
}

String[] cols = new String[projection.length];
Object[] values = new Object[projection.length];
int i = 0;
for (String col : projection) {
if (OpenableColumns.DISPLAY_NAME.equals(col)) {
cols[i] = OpenableColumns.DISPLAY_NAME;
values[i++] = file.getName();
} else if (OpenableColumns.SIZE.equals(col)) {
cols[i] = OpenableColumns.SIZE;
values[i++] = file.length();
}
}

cols = copyOf(cols, i);
values = copyOf(values, i);

final MatrixCursor cursor = new MatrixCursor(cols, 1);
cursor.addRow(values);
return cursor;
}
文章作者: HenryHaoson
文章链接: https://henryhaoson.github.io/2018/02/05/FileProvider/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 HenryHaoson
打赏
  • 微信
  • 支付寶