浏览代码

Fix image loading on Android Q

Vishnu Mohandas 5 年之前
父节点
当前提交
81d90da546
共有 5 个文件被更改,包括 39 次插入35 次删除
  1. 4 4
      lib/models/photo.dart
  2. 18 17
      lib/ui/thumbnail_widget.dart
  3. 15 12
      lib/ui/zoomable_image.dart
  4. 1 1
      pubspec.lock
  5. 1 1
      pubspec.yaml

+ 4 - 4
lib/models/photo.dart

@@ -57,11 +57,11 @@ class Photo {
     return photo;
     return photo;
   }
   }
 
 
-  AssetEntity getAsset() {
-    return AssetEntity(id: localId);
+  Future<AssetEntity> getAsset() {
+    return AssetEntity.fromId(localId);
   }
   }
 
 
-  Future<Uint8List> getBytes({int quality = 100}) {
+  Future<Uint8List> getBytes({int quality = 100}) async {
     if (localId == null) {
     if (localId == null) {
       return HttpClient().getUrl(Uri.parse(getRemoteUrl())).then((request) {
       return HttpClient().getUrl(Uri.parse(getRemoteUrl())).then((request) {
         return request.close().then((response) {
         return request.close().then((response) {
@@ -69,7 +69,7 @@ class Photo {
         });
         });
       });
       });
     } else {
     } else {
-      final originalBytes = getAsset().originBytes;
+      final originalBytes = (await getAsset()).originBytes;
       if (extension(title) == ".HEIC" || quality != 100) {
       if (extension(title) == ".HEIC" || quality != 100) {
         return originalBytes.then((bytes) {
         return originalBytes.then((bytes) {
           return FlutterImageCompress.compressWithList(bytes, quality: quality)
           return FlutterImageCompress.compressWithList(bytes, quality: quality)

+ 18 - 17
lib/ui/thumbnail_widget.dart

@@ -52,25 +52,26 @@ class _ThumbnailWidgetState extends State<ThumbnailWidget> {
         _imageProvider = Image.memory(cachedSmallThumbnail).image;
         _imageProvider = Image.memory(cachedSmallThumbnail).image;
         _hasLoadedThumbnail = true;
         _hasLoadedThumbnail = true;
       } else {
       } else {
-        widget.photo
-            .getAsset()
-            .thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE)
-            .catchError((e) {
+        widget.photo.getAsset().then((asset) {
+          asset
+              .thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE)
+              .then((data) {
+            if (data != null && mounted) {
+              final imageProvider = Image.memory(data).image;
+              precacheImage(imageProvider, context).then((value) {
+                if (mounted) {
+                  setState(() {
+                    _imageProvider = imageProvider;
+                    _hasLoadedThumbnail = true;
+                  });
+                }
+              });
+            }
+            ThumbnailLruCache.put(widget.photo, THUMBNAIL_SMALL_SIZE, data);
+          });
+        }).catchError((e) {
           _logger.warning("Could not load image: ", e);
           _logger.warning("Could not load image: ", e);
           _encounteredErrorLoadingThumbnail = true;
           _encounteredErrorLoadingThumbnail = true;
-        }).then((data) {
-          if (data != null && mounted) {
-            final imageProvider = Image.memory(data).image;
-            precacheImage(imageProvider, context).then((value) {
-              if (mounted) {
-                setState(() {
-                  _imageProvider = imageProvider;
-                  _hasLoadedThumbnail = true;
-                });
-              }
-            });
-          }
-          ThumbnailLruCache.put(widget.photo, THUMBNAIL_SMALL_SIZE, data);
         });
         });
       }
       }
     }
     }

+ 15 - 12
lib/ui/zoomable_image.dart

@@ -139,12 +139,13 @@ class _ZoomableImageState extends State<ZoomableImage>
       if (cachedThumbnail != null) {
       if (cachedThumbnail != null) {
         _onLargeThumbnailLoaded(Image.memory(cachedThumbnail).image, context);
         _onLargeThumbnailLoaded(Image.memory(cachedThumbnail).image, context);
       } else {
       } else {
-        widget.photo
-            .getAsset()
-            .thumbDataWithSize(THUMBNAIL_LARGE_SIZE, THUMBNAIL_LARGE_SIZE)
-            .then((data) {
-          _onLargeThumbnailLoaded(Image.memory(data).image, context);
-          ThumbnailLruCache.put(widget.photo, THUMBNAIL_LARGE_SIZE, data);
+        widget.photo.getAsset().then((asset) {
+          asset
+              .thumbDataWithSize(THUMBNAIL_LARGE_SIZE, THUMBNAIL_LARGE_SIZE)
+              .then((data) {
+            _onLargeThumbnailLoaded(Image.memory(data).image, context);
+            ThumbnailLruCache.put(widget.photo, THUMBNAIL_LARGE_SIZE, data);
+          });
         });
         });
       }
       }
     }
     }
@@ -156,12 +157,14 @@ class _ZoomableImageState extends State<ZoomableImage>
         final imageProvider = Image.file(cachedFile).image;
         final imageProvider = Image.file(cachedFile).image;
         _onFinalImageLoaded(imageProvider, context);
         _onFinalImageLoaded(imageProvider, context);
       } else {
       } else {
-        widget.photo.getAsset().file.then((file) {
-          if (mounted) {
-            final imageProvider = Image.file(file).image;
-            _onFinalImageLoaded(imageProvider, context);
-            ImageLruCache.put(widget.photo, file);
-          }
+        widget.photo.getAsset().then((asset) {
+          asset.file.then((file) {
+            if (mounted) {
+              final imageProvider = Image.file(file).image;
+              _onFinalImageLoaded(imageProvider, context);
+              ImageLruCache.put(widget.photo, file);
+            }
+          });
         });
         });
       }
       }
     }
     }

+ 1 - 1
pubspec.lock

@@ -344,7 +344,7 @@ packages:
       name: photo_manager
       name: photo_manager
       url: "https://pub.dartlang.org"
       url: "https://pub.dartlang.org"
     source: hosted
     source: hosted
-    version: "0.5.1"
+    version: "0.5.3+1"
   photo_view:
   photo_view:
     dependency: "direct main"
     dependency: "direct main"
     description:
     description:

+ 1 - 1
pubspec.yaml

@@ -23,7 +23,7 @@ dependencies:
   # The following adds the Cupertino Icons font to your application.
   # The following adds the Cupertino Icons font to your application.
   # Use with the CupertinoIcons class for iOS style icons.
   # Use with the CupertinoIcons class for iOS style icons.
   cupertino_icons: ^0.1.2
   cupertino_icons: ^0.1.2
-  photo_manager: ^0.5.1
+  photo_manager: ^0.5.3+1
   provider: ^3.1.0
   provider: ^3.1.0
   sqflite: ^1.3.0
   sqflite: ^1.3.0
   path_provider: ^1.6.5
   path_provider: ^1.6.5