如何在 Flutter 中获取 Asset 对象的路径?
Posted
技术标签:
【中文标题】如何在 Flutter 中获取 Asset 对象的路径?【英文标题】:How to get path of an Asset object in Flutter? 【发布时间】:2020-06-19 17:13:20 【问题描述】:我在颤振中使用多图像选择器,我必须将这些图像加载到文件中才能在照片视图中显示它们..
我确定我将资产转换为文件的方法不合适。 我无法做到这一点,因为我总是收到文件未找到异常。
这是我的代码:
import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:photo_view/photo_view.dart';
import 'package:flutter_absolute_path/flutter_absolute_path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
void main() => runApp(AddPost());
class AddPost extends StatefulWidget
@override
_MyAppState createState() => new _MyAppState();
class _MyAppState extends State<AddPost>
List<Asset> images = List<Asset>();
//File images;
List<File>files=List<File>();
String _error = 'No Error Dectected';
var width;
var height;
@override
void initState()
super.initState();
void getFileList() async
for (int i = 0; i < images.length; i++)
String filename=images[i].name;
String dir = (await getApplicationDocumentsDirectory()).path;
String path='$dir/$filename';
var path2 = await FlutterAbsolutePath.getAbsolutePath(path);
//var path = await images[i].filePath; ( the file path thing is not found)
print(path2);
var file = await getImageFileFromAsset(path2);
print(file);
files.add(file);
Future<File> getImageFileFromAsset(String path) async
final file = File(path);
return file;
Widget buildGridView()
return new Swiper(
loop: false,
itemCount: files==null?0:files.length,
pagination: new SwiperPagination(
// margin: new EdgeInsets.all(5.0),
builder: new DotSwiperPaginationBuilder(
color: Colors.purple, activeColor: Colors.green)),
itemBuilder: (BuildContext context, int index)
return Container(
child: PhotoView(
gaplessPlayback: false,
backgroundDecoration: BoxDecoration(color: Colors.transparent),
minScale: PhotoViewComputedScale.contained ,
maxScale: PhotoViewComputedScale.covered ,
imageProvider: ,
));
);
Future<void> loadAssets() async
List <Asset>resultList ;
try
resultList = await MultiImagePicker.pickImages(
maxImages: 6,
enableCamera: true,
selectedAssets: images,
cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
materialOptions: MaterialOptions(
actionBarColor: "#abcdef",
actionBarTitle: "Example App",
allViewTitle: "All Photos",
useDetailsView: false,
selectCircleStrokeColor: "#000000",
));
on Exception catch (e)
error = e.toString();
// 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;
_error = error;
getFileList();
);
@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(),
),
],
),
),
);
这些是我得到的错误:
java.io.FileNotFoundException:没有内容提供者:/data/user/0/xperience.flutter_app/app_flutter/Camera
除了:
未处理的异常:MissingPluginException(在通道 flutter_absolute_path 上找不到方法 getAbsolutePath 的实现)
感谢任何帮助。
【问题讨论】:
【参考方案1】:我通过放置 images[i].identifier 解决了我的问题
void getFileList() async
files.clear();
for (int i = 0; i < images.length; i++)
var path2 = await FlutterAbsolutePath.getAbsolutePath(images[i].identifier);
//var path = await images[i].filePath;
print(path2);
var file = await getImageFileFromAsset(path2);
print(file);
files.add(file);
【讨论】:
你好,是返回原图的路径还是复制到应用缓存目录的图片?以上是关于如何在 Flutter 中获取 Asset 对象的路径?的主要内容,如果未能解决你的问题,请参考以下文章
Vue.js - 如何通过 axios 响应数据从对象中获取特定字段
我们如何在 Flutter 中从 JSON 对象中获取特定数据?
Flutter小记2Android加载图片资源出现Unable to load asset的解决方案