Flutter如何在不填充内存的情况下从设备显示多个图像
我试图使用 local_image_provider 以提高Android设备的图像和视频地址, 检测到地址后,我将它们添加到列表中
List<DeviceImage> devideImgs = [];
getImage() async {
lip.LocalImageProvider imageProvider = lip.LocalImageProvider();
bool hasPermission = await imageProvider.initialize();
if (hasPermission) {
List<lip.LocalImage> images = await imageProvider.findLatest(100000);
images.forEach((image) async {
if (image.isImage) {
devideImgs.add(DeviceImage(image));
}
});
} else {
print("The user has denied access to images on their device.");
}
update();
}
,然后尝试在这样的GridView中显示这些图像,这
StaggeredGridView.countBuilder(
crossAxisCount: 3,
mainAxisSpacing: 3,
crossAxisSpacing: 3,
itemCount: devideImgs.length,
itemBuilder: (context, index) {
print('item $index created in grid view');
key: UniqueKey();
return Item(devideImgs[index]);
},
staggeredTileBuilder: (index) => StaggeredTile.count(1,1),
)
是我的 item
widget
Stack(
children: [
VisibilityDetector(
key: UniqueKey(),
onVisibilityChanged: (visibilityInfo) async {
},
child: Container(
child: GestureDetector(
onTap: () {},
child:
SizedBox(
width: double.maxFinite,
height: double.maxFinite,
child: Image(
image: widget.image,
fit: BoxFit.cover,
))
))),
],
);
widget.image.image
是devicemage
每件事都可以正常工作,但是当我滚动gridview时,我认为我正在填充内存,然后收到此 error
I/art (19971): Starting a blocking GC Alloc
I/art (19971): Alloc sticky concurrent mark sweep GC freed 87(4KB) AllocSpace objects,
0(0B) LOS objects, 15% free, 81MB/97MB, paused 1.224ms total 14.060ms
,然后应用程序崩溃了, 我使用了gridview.builder,但我认为这些物品和图像何时在屏幕上不可见,
请告诉我如何修复它
I tried to use local_image_provider to uptain image and video address of android device,
after detect address I add these to a list
List<DeviceImage> devideImgs = [];
getImage() async {
lip.LocalImageProvider imageProvider = lip.LocalImageProvider();
bool hasPermission = await imageProvider.initialize();
if (hasPermission) {
List<lip.LocalImage> images = await imageProvider.findLatest(100000);
images.forEach((image) async {
if (image.isImage) {
devideImgs.add(DeviceImage(image));
}
});
} else {
print("The user has denied access to images on their device.");
}
update();
}
and then tried to show these images in a gridview like this
StaggeredGridView.countBuilder(
crossAxisCount: 3,
mainAxisSpacing: 3,
crossAxisSpacing: 3,
itemCount: devideImgs.length,
itemBuilder: (context, index) {
print('item $index created in grid view');
key: UniqueKey();
return Item(devideImgs[index]);
},
staggeredTileBuilder: (index) => StaggeredTile.count(1,1),
)
this is my Item
widget
Stack(
children: [
VisibilityDetector(
key: UniqueKey(),
onVisibilityChanged: (visibilityInfo) async {
},
child: Container(
child: GestureDetector(
onTap: () {},
child:
SizedBox(
width: double.maxFinite,
height: double.maxFinite,
child: Image(
image: widget.image,
fit: BoxFit.cover,
))
))),
],
);
that widget.image
is a DeviceImage
every thing work good but when I scroll gridview I think I'm filling up memory and then I recieve this error
I/art (19971): Starting a blocking GC Alloc
I/art (19971): Alloc sticky concurrent mark sweep GC freed 87(4KB) AllocSpace objects,
0(0B) LOS objects, 15% free, 81MB/97MB, paused 1.224ms total 14.060ms
and then app crashed,
I used Gridview.builder but I think the Items and images dont clear when these are not visible in screen
Please tell me how can I fix it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大家好,我通过替换来解决这个问题
,
此代码来自照片库
Hey guys I solved this problem by replacing
with
and this code is from photo gallery package