获取 Android 上的照片库列表
我在找: 现有照片库名称的列表(希望也是它们的顶部缩略图) 图库的内容(然后我可以根据需要加载缩略图和全尺寸)
我将如何获取“图库”列表(不知道这是否是 android 中用于显示图像分组的正确术语)图库应用程序...)及其内容?我需要访问其结构中的画廊,而不使用现有的画廊显示(我正在创建一个全新的画廊,而不是照片请求者的覆盖层等)。
我认为 MediaStore.Images 是我需要的地方,但我不知道没有看到任何可以给我分组的东西......
I'm looking for:
A list of the existing photo gallery names (hopefully their top thumbnail as well)
The contents of the gallery (I can then load thumbnails and full size as needed)
How would I go about getting a list of the "Galleries" (don't know if that's the proper term in android for the groupings of images visible in the Gallery app...) and their contents? I need access to the gallery in it's structure without using the existing gallery display (I'm creating a totally new one, not an over layer to the photo requestor etc.)
I assume MediaStore.Images is where I need to be but I don't see anything that will give me the groupings...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
分组由
MediaStore.Images.Media.BUCKET_DISPLAY_NAME
定义。以下是列出图像并记录其存储桶名称和 date_taken 的示例代码:Groupings are defined by
MediaStore.Images.Media.BUCKET_DISPLAY_NAME
. Here is the sample code to list the images and log their bucket name and date_taken:以下是几个简单步骤的完整解决方案:
接下来的几个步骤将指导您如何创建一个
Vector
来保存在给定设备上找到的相册。每个相册都将包含一个预览图像以及一个用于保存相册所有图像的内部Vector
。创建一个对象,用于保存从存储中提取的图像。我们将其命名为
PhoneAlbum
。它将如下所示:创建一个对象,用于保存相册中的图像,名为:
PhonePhoto
创建一个接口来处理完成后提取的图像。我们将其命名为
OnPhoneImagesObtained
。这是:创建一个新类:
DeviceImageManager
创建
DeviceImageManager
后,添加以下方法:现在剩下的就是将图像渲染到屏幕上。就我而言,我喜欢使用
Picasso
。我是这样做的:不要忘记在清单中添加读取外部存储的权限:
就是这样。你可以走了!
祝你好运!
Here is the full solution in few simple steps:
The next few steps will guid you how to create a
Vector
that will hold the albums found on a given device. Every Album will hold a preview image as well an innerVector
that will hold all the images of the Album.Create an object that will hold the images once extracted from storage. We are going to call it
PhoneAlbum
. This is how it's going to look:Create an object that will hold the images within the album called:
PhonePhoto
Create an interface to handle the extracted images upon completion. We are going to call it
OnPhoneImagesObtained
. Here it is:Create a new class:
DeviceImageManager
Once you created
DeviceImageManager
, add the following method:Now all you have left is to render the images to screen. In my case I like to use
Picasso
. Here is how I do it:Don't forget to add a permission to read external storage in your manifest:
That's it. You are good to go!
Good luck!
我找到了一种无需迭代每张照片即可获取相册的方法。
光标将包含尽可能多的元素,因为存在不同的存储桶名称,并且您还可以在每个光标位置内获取计数,以获取相册内图像的计数
示例:
有关选择参数的一些解释:
contentResolver 在编译 sqlLite 的结果查询时添加括号,因此如果我们做出像
“GROUP BY”+MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME
这样的选择,它将被编译为“WHERE(GROUP BYbucket_display_name)”并在运行时导致SQLiteException。否则,如果我们进行像
“1) GROUP BY (” + MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME
这样的选择,它将被编译为“WHERE (1) GROUP BY (bucket_display_name)”,这是正确的
I've found a way to get albums without iterating over every photo.
cursor will contain as much elements, as distinct bucket name exists, and also you can get count inside every cursor position to get count of images inside album
here example:
Some explanation about selection param:
contentResolver adds parentheses when compiling resulting query for sqlLite, so if we make selection like
"GROUP BY " + MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME
it will be compiled as "WHERE (GROUP BY bucket_display_name)" and will cause SQLiteException at runtime. Otherwise if we make selection like
"1) GROUP BY (" + MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME
it will be compiled as "WHERE (1) GROUP BY (bucket_display_name)", which is correct
从这里下载源代码(以编程方式从 Android 中的图库中获取所有图像)
activity_main.xml
MainActivity.java
Download the source code from here (Get all images from gallery in android programmatically)
activity_main.xml
MainActivity.java
对于使用 Compose 的 Android,这也很有效!
我创建了一个库来使这更容易:
https://github.com/nabla-run/Compose-gallery-picker
For Android with Compose, this works well too!
I have created a library to make this easier:
https://github.com/nabla-run/Compose-gallery-picker