Android通过内容提供器获取相册中所有图片

Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
    byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    String filePath = new String(data, 0, data.length - 1);
}

这样就获取了,输出如下

12-27 11:22:20.267 2206-2206/com.example.wechat I/xbh: /storage/emulated/0/Pictures/after_choose_pic.png
12-27 11:22:20.267 2206-2206/com.example.wechat I/xbh: /storage/emulated/0/Download/after_choose_pic.png
12-27 11:22:20.267 2206-2206/com.example.wechat I/xbh: /storage/emulated/0/DCIM/Camera/after_choose_pic.png
12-27 11:22:20.267 2206-2206/com.example.wechat I/xbh: /storage/emulated/0/DCIM/after_choose_pic.png
12-27 11:22:20.267 2206-2206/com.example.wechat I/xbh: /storage/emulated/0/after_choose_pic.png
12-27 11:22:20.267 2206-2206/com.example.wechat I/xbh: /storage/emulated/0/TIM??20171227233044.png


不过会出了一个权限的问题,你需要在注册文件中声明READ_EXTERNAL_STORAGE,此外还要动态声明权限

if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {
    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}

有关动态声明权限,更详细可以看http://blog.csdn.net/qq_36523667/article/details/78917460

有关获取相册更多信息:图片名,图片注释等,可以看http://blog.csdn.net/bi_diu1368/article/details/52425999

你可能感兴趣的:(Android)