Created
August 28, 2024 11:17
-
-
Save H3mnz/b5c4c2523f42b9839a67e25d441243b0 to your computer and use it in GitHub Desktop.
Get Thumbnail from File in Windows with Dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:developer'; | |
import 'dart:typed_data'; | |
import 'package:windows_storage/windows_storage.dart'; | |
class ThumbnailGenerator { | |
ThumbnailGenerator._(); | |
static Future<Uint8List> generate(String path) async { | |
try { | |
final storageFile = await StorageFile.getFileFromPathAsync(path); | |
if (storageFile == null) throw Exception('File Not Exists.'); | |
final thumbnail = await storageFile.getThumbnailAsyncOverloadDefaultSizeDefaultOptions(ThumbnailMode.singleItem); | |
if (thumbnail == null) throw Exception('Thumbnail Not Exists.'); | |
DataReader reader = DataReader.createDataReader(thumbnail.getInputStreamAt(0)); | |
await reader.loadAsync(thumbnail.size); | |
return Uint8List.fromList(reader.readBytes(thumbnail.size)); | |
} catch (e) { | |
log(e.toString()); | |
rethrow; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment