简体   繁体   English

如何在 Flutter 中获取 Asset 对象的路径?

[英]How to get path of an Asset object in Flutter?

I am using multi-image picker in flutter and i have to load these images in a file to display them in a Photo View..我在颤振中使用多图像选择器,我必须将这些图像加载到文件中才能在照片视图中显示它们。

I am certain that my method to convert the assets to files is not appropriate.我确信我将资产转换为文件的方法不合适。 I am not able to achieve this as i am always getting file not found exception .我无法实现这一点,因为我总是收到文件未找到异常。

This is my code:这是我的代码:

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(),
        ),

      ],
    ),
  ),
);
 }
 }

These are the errors im getting:这些是我得到的错误:

java.io.FileNotFoundException: No content provider: /data/user/0/xperience.flutter_app/app_flutter/Camera java.io.FileNotFoundException:无内容提供者:/data/user/0/xperience.flutter_app/app_flutter/Camera

in addition to:此外:

Unhandled Exception: MissingPluginException(No implementation found for method getAbsolutePath on channel flutter_absolute_path)未处理的异常:MissingPluginException(在通道 flutter_absolute_path 上找不到方法 getAbsolutePath 的实现)

Any help is appreciated.任何帮助表示赞赏。

I resolved my issue by putting images[i].identifier我通过放置 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);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM