搜索插件、代码仓与 APP 分类

插件详情v0.1.5

media_gallery

一个列出原生相册项的 Flutter 插件。

近 30 天下载
36
喜欢数
31
Pub 分数
30

安装插件

flutter pub add media_gallery

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

media_gallery 包是一个 Flutter 插件,可访问原生设备相册项目,允许用户浏览并从设备的本地媒体存储中选择图片和视频。它提供了一个简单的接口来获取带有文件路径、时间戳和尺寸等元数据的媒体文件。该插件专为与 Flutter 应用无缝集成而设计,支持 Android 和 iOS 的平台特定实现。对于需要媒体选择功能的应用程序(如照片分享、个人资料图片上传或内容创作工具)非常理想。它依赖于原生 API 以确保性能和兼容性。

适合场景

  • 从设备图库中选择图片和视频
  • 个人资料图片上传流程
  • 媒体丰富的应用内容创作
  • 在Flutter应用中浏览照片相册
  • 将原生媒体选择器集成到Flutter UI

主要特点

  • 访问原生设备媒体文件
  • 同时支持图片和视频
  • 检索媒体元数据(路径、时间戳、大小)
  • 跨平台兼容性(Android/iOS)
  • 用于列出和筛选媒体的简单 API

适合对象

  • 需要媒体选择的Flutter应用
  • 社交媒体或照片分享平台
  • 内容创作工具
  • 包含用户生成媒体上传的应用
  • 开发自定义媒体选择器的开发者

使用建议

  • 需要适当的权限(相机和存储)
  • 仅限原生相册;不支持云媒体
  • 平台特定的行为可能有所不同
  • 无内置UI组件;需要自定义实现
  • 大型媒体库可能需要额外的处理

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Media Gallery plugin for Flutter

pub package

A Flutter plugin that lists native gallery items.

Installation

First, add media_gallery as a dependency in your pubspec.yaml file.

iOS

Add the following keys to your Info.plist file, located in <project root>/ios/Runner/Info.plist:

<key>NSPhotoLibraryUsageDescription</key>
<string>Example usage description</string>
Android

Add the following permissions to your AndroidManifest.xml, located in <project root>/android/app/src/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

You should also manage those permissions (for example thanks to the permission_handler plugin). You can see an example for more details.

Usage
Listing media collections
final List<MediaCollection> collections = await MediaGallery.listMediaCollections(
    mediaTypes: [MediaType.image, MediaType.video],
);
Listing medias in a collection
final MediaPage imagePage = await collection.getMedias(
    mediaType: MediaType.image,
    take: 500,
);
final MediaPage videoPage = await collection.getMedias(
    mediaType: MediaType.video,
    take: 500,
);
final List<Media> allMedias = [
    ...imageRange.items,
    ...videoRange.items,
]
..sort((x, y) => y.creationDate.compareTo(x.creationDate));
Loading more medias in a collection
if (!imagePage.isLast) {
    final nextImagePage = await imagePage.nextPage();
    // ...
}
Getting a file
final File file = await media.getFile();
Getting thumbnail data
final List<int> data = await media.getThumbnail();
Displaying thumbnails

MediaThumbnailProvider, MediaCollectionThumbnailProvider are available to display thumbnail images (here with the help of transparent_image) :

FadeInImage(
    fit: BoxFit.cover,
    placeholder: MemoryImage(kTransparentImage),
    image: MediaThumbnailProvider(
        media: media,
    ),
)
Displaying medias

You can use MediaImageProvider to display an image (here with the help of transparent_image):

FadeInImage(
    fit: BoxFit.cover,
    placeholder: MemoryImage(kTransparentImage),
    image: MediaImageProvider(
        media: media,
    ),
)

To display a video, you can use video_player.

Image/Video picker

picker

You can build a gallery picker easily with this package.

See the example for more details.

所属分类

源码仓库

使用此插件的项目