gpt4 book ai didi

flutter - 如何在 Flutter 中获取 Assets 对象的路径?

转载 作者:行者123 更新时间:2023-12-04 14:15:38 28 4
gpt4 key购买 nike

我在 flutter 中使用多图像选择器,我必须将这些图像加载到一个文件中以在照片 View 中显示它们。

我确定我将 Assets 转换为文件的方法不合适。我无法实现这一点,因为我总是收到找不到文件的异常。

这是我的代码:

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(未在 channel flutter_absolute_path 上找到方法 getAbsolutePath 的实现)

感谢任何帮助。

最佳答案

我通过放置 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 - 如何在 Flutter 中获取 Assets 对象的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60573018/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com