有没有办法从手机图库中读取缩略图?
Posted
技术标签:
【中文标题】有没有办法从手机图库中读取缩略图?【英文标题】:Is there a way I can read thumbnails from phone gallery? 【发布时间】:2019-02-17 21:53:04 【问题描述】:我正在尝试通过读取外部文件目录以及可能以 jpg 或 png 结尾的每个图像来自己显示所有手机图库图像。我实现了这一点,但由于它们的大小或编号,无法将它们全部显示在网格中。图像,应用程序崩溃。代码看起来有点像这样..
new GridView.count(
shrinkWrap: true,
physics: new ClampingScrollPhysics(),
crossAxisCount: 2,
// children: new List<Widget>.generate(_images.length, (index)
// children: new List<Widget>.generate(allImages.length, (index)
children: new List<Widget>.generate(_PhoneImages.length, (index)
File imgFile = _phoneImageFiles[index];
thumbBytes = _phoneThumbBytes[index]; // assuming it got created!!!
// print('thumbbytes $thumbBytes');
print('phone image index: $index');
return new GridTile(
child: new GestureDetector(
child: new Stack(
children: [
new Card(
// color: Colors.blue.shade200,
color: Colors.white70,
child: new Center(
// child: new Text('tile $index'),
// child: new Image.asset(_images[index]),
/*
child: new CachedNetworkImage(
imageUrl: allImages[index].path,
// placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error),
)
*/
child: new Image.file(imgFile,
// child: new Image.memory(thumbBytes,
所以我尝试了 imageresize 库,它告诉我要进行繁重的调整大小操作,这需要将近 20 分钟才能显示缩略图。
我所需要的只是从画廊中读取缩略图,就像手机画廊的显示方式一样。我不需要分类。我需要所有内容和完整版本的链接,以便以后可以对它们进行操作。
【问题讨论】:
【参考方案1】:我认为这可能会有所帮助multi_image_picker
例如
import 'package:flutter/material.dart';
import 'package:multi_image_picker/asset.dart';
class AssetView extends StatefulWidget
final int _index;
final Asset _asset;
AssetView(this._index, this._asset);
@override
State<StatefulWidget> createState() => AssetState(this._index, this._asset);
class AssetState extends State<AssetView>
int _index = 0;
Asset _asset;
AssetState(this._index, this._asset);
@override
void initState()
super.initState();
_loadImage();
void _loadImage() async
await this._asset.requestThumbnail(300, 300); // here requesting thumbnail
setState(() );
@override
Widget build(BuildContext context)
if (null != this._asset.thumbData)
return Image.memory(
this._asset.thumbData.buffer.asUint8List(),
fit: BoxFit.cover,
);
return Text(
'$this._index',
style: Theme.of(context).textTheme.headline,
);
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:multi_image_picker/asset.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'asset_view.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget
@override
_MyAppState createState() => new _MyAppState();
class _MyAppState extends State<MyApp>
List<Asset> images = List<Asset>();
String _error;
@override
void initState()
super.initState();
Widget buildGridView()
return GridView.count(
crossAxisCount: 3,
children: List.generate(images.length, (index)
return AssetView(index, images[index]);
),
);
Future<void> loadAssets() async
setState(()
images = List<Asset>();
);
List resultList;
String error;
try
resultList = await MultiImagePicker.pickImages(
maxImages: 300,
);
on PlatformException catch (e)
error = e.message;
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(()
images = resultList;
if (error == null) _error = 'No Error Dectected';
);
@override
Widget build(BuildContext context)
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: <Widget>[
Center(child: Text('Error: $_error')),
RaisedButton(
child: Text("Pick images"),
onPressed: loadAssets,
),
Expanded(
child: buildGridView(),
)
],
),
),
);
【讨论】:
看起来很整洁。不过,我不想选。当我在网格中打开页面时,我需要简单地显示。我已经得到了它们,但是它们太大了,因为我使用 Image.file(/path/to/file) 直接从文件中加载它们。使用 Image 包从内存中创建缩略图,但这是一个昂贵的过程并且需要大量时间。有什么方法可以随心所欲吗? 我不知道那么抱歉。以上是关于有没有办法从手机图库中读取缩略图?的主要内容,如果未能解决你的问题,请参考以下文章