oynix

于无声处听惊雷,于无色处见繁花

Android获取系统相册所有图片


直接获取所有照片的信息,而不是打开照片选择页面

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
32
33
34
35
36
37
38
39
 private ArrayList<CategoryFile> queryCategoryFilesSync(FileType type) {
ArrayList<CategoryFile> files = new ArrayList<>();
Uri uri = MediaStore.Images.Media.getContentUri("external");
if (uri != null) {
String[] projection = new String[]{FileColumns._ID, // id
FileColumns.DATA, // 文件路径
FileColumns.SIZE, // 文件大小
FileColumns.DATE_MODIFIED}; // 修改日期
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
final int pathIdx = cursor
.getColumnIndex(FileColumns.DATA);
final int sizeIdx = cursor
.getColumnIndex(FileColumns.SIZE);
final int modifyIdx = cursor
.getColumnIndex(FileColumns.DATE_MODIFIED);
do {
String path = cursor.getString(pathIdx);
CategoryFile file = new CategoryFile();
file.mType = type;
file.mPath = path;
file.mParent = FileUtil.getPathFromFilepath(file.mPath);
file.mName = FileUtil.getNameFromFilepath(file.mPath);
file.mSize = cursor.getLong(sizeIdx);
file.mLastModifyTime = cursor.getLong(modifyIdx);
files.add(file);
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
}
}
}
return files;
}

其中CategoryFile为存储照片信息的Bean类

FileUtil类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class FileUtil {

private static final char UNIX_SEPARATOR = '/';

public static String getPathFromFilepath(String filepath) {
if (!TextUtils.isEmpty(filepath)) {
int pos = filepath.lastIndexOf(UNIX_SEPARATOR);
if (pos != -1) {
return filepath.substring(0, pos);
}
}
return "";
}

public static String getNameFromFilepath(String filepath) {
if (!TextUtils.isEmpty(filepath)) {
int pos = filepath.lastIndexOf(UNIX_SEPARATOR);
if (pos != -1) {
return filepath.substring(pos + 1);
}
}
return "";
}
}
------------- (完) -------------
  • 本文作者: oynix
  • 本文链接: https://oynix.net/2017/06/70f814330051/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

欢迎关注我的其它发布渠道