簡體   English   中英

運行 image_picker 時我的新 android 應用程序崩潰(基於 flutter dart)

[英]My new android app crash when running image_picker (base on flutter dart)

我正在開發一個使用 Flutter 購物的應用程序,所以我在某個地方堆放東西,我需要幫助。

我使用 pub.dev ( https://pub.dev/packages/image_picker#-readme-tab- ) 上提供的 image_picker 代碼然后我開發了一個頁面來在我的應用程序上添加產品,所以當我點擊相機圖標或圖庫時圖標選擇圖像應用程序崩潰並打開相機屏幕/畫廊屏幕。

我的大問題是; 它在模擬器上運行良好,但在真正的手機上,它會崩潰。

我嘗試使用 pub.dev 上所示的retrieveLostData(),但我不知道在哪里使用它。

波紋管是我的代碼

import 'package:flutter/material.dart';
import 'dart:io';
import 'package:flutter/widgets.dart';

import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart';

//my imports
import 'package:myshop/main.dart';

class ImageCapture extends StatefulWidget {
  @override
  _ImageCaptureState createState() => _ImageCaptureState();
}

class _ImageCaptureState extends State<ImageCapture> {
  File _imageFile;

  Future<void> _cropImage() async {
    File cropped = await ImageCropper.cropImage(
        sourcePath: _imageFile.path,);

    setState(() {
      _imageFile = cropped ?? _imageFile;
    });
  }
 // pick image from galery or camera
  Future<void> _pickImage(ImageSource source) async {
    File selected = await ImagePicker.pickImage(source: source);

    setState(() {
      _imageFile = selected;
    });
  }
   //remove image
  void _clear() {
    setState(() => _imageFile = null);
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        backgroundColor: Colors.orange[700],
        title: Text(
          'Image Capture',
          style: TextStyle(fontFamily: 'Exo', fontSize: 13.0),
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.search,
              color: Colors.white,
              size: 23.0,
            ),
            onPressed: () {},
          ),
          IconButton(
              icon: Icon(
                Icons.home,
                size: 18,
                color: Colors.white,
              ),
              onPressed: () => Navigator.of(context)
                  .push(MaterialPageRoute(builder: (context) => Home())))
        ],
      ),

      //pick image from camera or gallery
      bottomNavigationBar: BottomAppBar(
        color: Colors.cyan[900],
        child: Container( margin: EdgeInsets.fromLTRB(30, 0, 30, 0),
          child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[

              IconButton(
                icon: Icon(Icons.photo_camera, color: Colors.orange, size: 18,),
                onPressed: () => _pickImage(ImageSource.camera),
              ),
              IconButton(
                icon: Icon(Icons.photo_library, color: Colors.orange, size: 18,),
                onPressed: () => _pickImage(ImageSource.gallery),
              ),
            ],
          ),
        ),
      ),

      body: ListView(
        children: <Widget>[
          if (_imageFile != null) ...[
            Image.file(_imageFile),
            Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                FlatButton(
                  child: Icon(Icons.crop, size: 18,),
                  onPressed: _cropImage,
                ),
                FlatButton(
                  child: Icon(Icons.refresh,size: 18,),
                  onPressed: _clear,
                ),
              ],
            ),
          ], if (_imageFile==null)...[
            Center(
              child: Text('No Image Captured', style: TextStyle(color: Colors.black54),),
            )
          ]
        ],
      ),
    );
  }
}

您需要在項目的AndroidManifest.xml文件中添加camerastorage permission

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

此外,您需要檢查活動中兩者的運行時權限。

前往Marshmallow 中的存儲權限錯誤了解更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM