Flutter 图像选择器不会出现在 Android 10 中

Posted

技术标签:

【中文标题】Flutter 图像选择器不会出现在 Android 10 中【英文标题】:Flutter image picker won't show up in Android 10 【发布时间】:2020-12-26 09:25:06 【问题描述】:

我是 Flutter 的初学者。我正在检查是否在颤振中使用 image_picker。但是当我尝试用我的 Andorid Phone (10) 检查 image_picker 时,它不会出现。不知道怎么回事,我在androidManifest.xml中添加了android:requestLegacyExternalStorage="true"

仍然没有变化,我不知道该怎么办?请帮帮我

    import 'package:flutter/material.dart';
    import 'package:image_picker/image_picker.dart';
    import 'package:path/path.dart';
    import 'package:path_provider/path_provider.dart';

    void main() 
      runApp(MyApp());
    

    class MyApp extends StatelessWidget 
      @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  


class MyHomePage extends StatefulWidget 
  @override
  _MyHomePageState createState() => _MyHomePageState();


class _MyHomePageState extends State<MyHomePage> 
  File _fileimage;

  final _picker = ImagePicker();

  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              _fileimage != null
                  ? FileImage(_fileimage)
                  : Text('No image selected'),
              RaisedButton(
                onPressed: () => _takePicture,
                child: Text('hi'),
              )
            ],
          ),
        ),
      ),
    );
  

  void _takePicture() async 
    final pickedImage = await _picker.getImage(source: ImageSource.gallery);

    // if (pickedImage == null) 
    //   return;
    // 

    File tmpFile = File(pickedImage.path);

    final Directory directory = await getApplicationDocumentsDirectory();
    final String path = directory.path;

    final String fileName =
        basename(pickedImage.path); // Filename without extension
    final String fileExtension = extension(pickedImage.path); // e.g.

    tmpFile = await tmpFile.copy('$path/$fileName$fileExtension');
    setState(() => _fileimage = tmpFile);
  

【问题讨论】:

调试时有什么错误? 【参考方案1】:

我发现您的代码有问题。 您在 'Widget' 中使用了 'FileImage' 是必需的。因为 'FileImage' 返回 'ImageProvider',你应该使用 'Image.file()' 而不是 'FileImage'。

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

void main() 
  runApp(MyApp());


class MyApp extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  


class MyHomePage extends StatefulWidget 
  @override
  _MyHomePageState createState() => _MyHomePageState();


class _MyHomePageState extends State<MyHomePage> 
  File _fileimage;

  final _picker = ImagePicker();

  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              _fileimage != null
                  ? Image.file(_fileimage)
                  : Text('No image selected'),
              RaisedButton(
                onPressed: () async 
                  _takePicture();
                ,
                child: Text('hi'),
              )
            ],
          ),
        ),
      ),
    );
  

  void _takePicture() async 
    final pickedImage = await _picker.getImage(source: ImageSource.gallery);

    // if (pickedImage == null) 
    //   return;
    // 

    File tmpFile = File(pickedImage.path);

    final Directory directory = await getApplicationDocumentsDirectory();
    final String path = directory.path;

    final String fileName =
        basename(pickedImage.path); // Filename without extension
    final String fileExtension = extension(pickedImage.path); // e.g.

    tmpFile = await tmpFile.copy('$path/$fileName$fileExtension');
    setState(() => _fileimage = tmpFile);
  

【讨论】:

有没有办法使用 Image.file() 来适应屏幕宽度 Container( decoration: BoxDecoration( color: Colors.black, image: DecorationImage( image: FileImage(_fileimage), fit: BoxFit.contain, ), ), )

以上是关于Flutter 图像选择器不会出现在 Android 10 中的主要内容,如果未能解决你的问题,请参考以下文章

Flutter/Dart - 如何在其他屏幕上的图像选择器后更新图像

Flutter- 图像选择器包:通过删除操作一张一张地显示图像

Flutter 表单构建器包图像选择器 Firestore Flutter

适用于 Web 和移动设备的 Flutter 图像选择器

Flutter 图像选择器在带有 Mac M1 芯片模拟器的 ios 14 中不起作用

Flutter ios 设备不会从 FCM 通知触发 onMessage。实施 APN 时 Sendtodevice 失败