选择图像后如何获取图像路径

选择图像后如何获取图像路径

本文介绍了在Flutter中如何使用"Multi Image Picker"选择图像后如何获取图像路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从选定的多个图像中获取一个图像路径,我正在使用此链接到选择多张图片,但是我有资产,我想要从多张图片中选择路径,因为我想上传到API.我在pubspec.yaml中添加了此依赖项,如果有什么好的方法可以做到,请告诉我

I want to get an image path from selected multiple images, I'm using this link to select multiple images but I got assets, I want paths from selected multiple images because I want to upload into the API.I added this dependency in pubspec.yaml If there any good way to do this, please tell me

multi_image_picker: ^4.6.3

这是我的文件上传类,该用户界面类似于Facebook.

This is my file upload class, This UI looks similar to Facebook.

import 'dart:typed_data';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:multi_image_picker/multi_image_picker.dart';

class UpdateStatus extends StatefulWidget {
  @override
  _UpdateStatusState createState() => _UpdateStatusState();
}

class _UpdateStatusState extends State<UpdateStatus> {
  List<Asset> images = List<Asset>();
  String _error = 'No Error Dectected';

  Future<ByteData> byteData;
 // List<int> imageData = byteData.buffer.asUint8List();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Create Post'),
        actions: <Widget>[
          Padding(
            padding: const EdgeInsets.all(18.0),
            child: InkWell(child: Text('POST',style: TextStyle(fontSize: 18.0),),onTap: ()
              {
                print('Post this post');
              },),
          )
        ],
      ),
      body: SingleChildScrollView(
        child: Container(
          height: MediaQuery.of(context).size.height,
          width:MediaQuery.of(context).size.width ,
          color: Colors.white,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Container(
                height: 300.0,
                color: Colors.white,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextFormField(
                    keyboardType: TextInputType.multiline,
                    maxLines: 100,
                      style: new TextStyle(
                          fontSize: 18.0,
                          color: Colors.black
                      ),
                    decoration: InputDecoration(
                      hintText: 'Enter your Post Details Here !',
                      border: InputBorder.none,
                    ),
                  ),
                ),
              ),
              Divider(
                thickness: 1.0,
              ),
              Column(
                children: <Widget>[
                  Container(
                    height: 40.0,
                    color: Colors.white70,
                    child: Padding(
                      padding:EdgeInsets.only(left: 18.0,),
                      child: InkWell(
                        child: Row(
                          children: <Widget>[
                            Icon(Icons.add_a_photo,),
                            Text("  Choose Image",style: TextStyle(fontSize: 24.0,),),
                          ],
                        ),
                        onTap: ()
                        {
                          print(images.toList().toString());
                          print('choose image from local');
                        },
                      ),
                    ),
                  ),
                  Divider(
                    thickness: 1.0,
                  ),
                  Container(
                    height: 40.0,
                    color: Colors.white70,
                    child: Padding(
                      padding:EdgeInsets.only(left: 18.0,),
                      child: InkWell(
                        child: Row(
                          children: <Widget>[
                            Icon(Icons.add_photo_alternate,),
                            Text("  Choose Video",style: TextStyle(fontSize: 24.0,),),
                          ],
                        ),
                        onTap: ()
                        {
                          print('choose video from local');
                        },
                      ),
                    ),
                  ),
                ],
              ),
              Divider(
                thickness: 1.0,
              ),
         Container(
           height: 200,
              child: Column(
                children: <Widget>[
                  Center(child: Text('Error: $_error')),
                  RaisedButton(
                    child: Text("Pick images"),
                    onPressed: loadAssets,
                  ),
                  Expanded(
                    child: buildGridView(),
                  )
                ],
              ),
         ),
         /*
          Column(
            children: <Widget>[
              Center(child: Text('Error: $_error')),
              RaisedButton(
                child: Text("Pick images"),
                onPressed: loadAssets,
              ),
              Expanded(
                child: buildGridView(),
              )
            ],
          ),

              */
            ],
          ),
        ),
      ),
    );
  }

  Future<void> loadAssets() async {

    List<Asset> resultList = List<Asset>();
    String error = 'No Error Dectected';

    ByteData byteData;


    try {
      resultList = await MultiImagePicker.pickImages(
        maxImages: 300,
        enableCamera: true,
        selectedAssets: images,
        cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
        materialOptions: MaterialOptions(
          actionBarColor: "#abcdef",
          actionBarTitle: "Ilma",
          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;
      print('000000000000000000000');
      print('000000000000000000000');
      print(images);
      print('000000000000000000000');
      print('000000000000000000000');
    });



  }

  Widget buildGridView() {
    return GridView.count(
      crossAxisCount: 3,
      children: List.generate(images.length, (index) {
        Asset asset = images[index];

        byteData=asset.getByteData();

        print('0000');
        print(byteData);
        print('0000');



        return AssetThumb(
          asset: asset,
          width: 300,
          height: 300,
        );
      }),
    );
  }

}

推荐答案

Padding(
            padding: const EdgeInsets.all(18.0),
            child: InkWell(
              child: Text(
                'POST',
                style: TextStyle(fontSize: 18.0),
              ),
              onTap: () async {

 List<MultipartFile> multipart = List<MultipartFile>();

                for (int i = 0; i < images.length; i++) {
                  var path = await FlutterAbsolutePath.getAbsolutePath(images[i].identifier);

                }

              },
            ),
          )

这篇关于在Flutter中如何使用"Multi Image Picker"选择图像后如何获取图像路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:02