You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Kotlin应用中实现图片与视频按文件夹分类展示

Folder-Based Media Grouping with Smooth App Performance

To implement folder-organized display of images and videos while keeping your app responsive, follow these practical steps focused on efficient media querying, caching, and UI optimization:

1. Fetch Media with Folder Context

Instead of retrieving media items in a flat list, extract the parent folder from each media file's path to group items.

Android Example (Using MediaStore):

// Define necessary projection to avoid unnecessary data load
String[] mediaProjection = {
    MediaStore.MediaColumns._ID,
    MediaStore.MediaColumns.DISPLAY_NAME,
    MediaStore.MediaColumns.DATA,
    MediaStore.MediaColumns.DATE_ADDED
};

// Query both images and videos (or separate queries for each)
Uri mediaUri = MediaStore.Files.getContentUri("external");
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + " IN (" 
    + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE + ", " 
    + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO + ")";

Cursor cursor = getContentResolver().query(
    mediaUri,
    mediaProjection,
    selection,
    null,
    MediaStore.MediaColumns.DATE_ADDED + " DESC"
);

if (cursor != null) {
    HashMap<String, List<MediaItem>> folderMediaMap = new HashMap<>();
    int dataColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);

    while (cursor.moveToNext()) {
        String filePath = cursor.getString(dataColumnIndex);
        File parentFolder = new File(filePath).getParentFile();
        if (parentFolder == null) continue;

        String folderName = parentFolder.getName();
        // Skip system/hidden folders if needed
        if (folderName.startsWith(".")) continue;

        MediaItem item = new MediaItem(
            cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID)),
            cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME)),
            filePath
        );

        folderMediaMap.computeIfAbsent(folderName, k -> new ArrayList<>()).add(item);
    }
    cursor.close();
    // Use folderMediaMap to populate your folder list UI
}

iOS Example (Using Photos Framework):

For iOS, the Photos API natively supports folder/album grouping. Use PHAssetCollection to fetch user-created folders:

let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType IN %@", [PHAssetMediaType.image, PHAssetMediaType.video])

// Fetch all user albums/folders
let collections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
collections.enumerateObjects { collection, _, _ in
    let assets = PHAsset.fetchAssets(in: collection, options: fetchOptions)
    // Map collection to its assets for UI display
}

2. Optimize Performance with Caching

  • In-Memory Cache: Store the folder-to-media mapping in a HashMap (Android) or Dictionary (iOS) to avoid re-querying the media store on every UI refresh.
  • Media Change Listener: Register a ContentObserver (Android) or PHPhotoLibraryChangeObserver (iOS) to update the cache only when media is added/removed, instead of full re-scans.
  • Thumbnail Caching: Use libraries like Glide (Android) or Kingfisher (iOS) to cache thumbnails, reducing repeated disk I/O.

3. UI Smoothness Tips

  • RecyclerView/UICollectionView: Use these components with view recycling to minimize memory usage during scroll.
  • Lazy Loading: Load media items of a folder only when the user expands it, instead of loading all items upfront.
  • Background Threads: Run all media querying and thumbnail loading tasks in background threads to avoid blocking the main thread.

4. Handle Large Media Libraries

  • Pagination: For folders with hundreds of items, implement pagination to load chunks of media at a time.
  • Filter System Folders: Exclude folders like Android/data or hidden folders to reduce the number of items processed.

内容的提问来源于stack exchange,提问作者Dark Light Moon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 13:07:22