首页 > 解决方案 > 从图库中挑选图像时丢失与设备的连接错误

问题描述

我正在制作应用程序,在其中我从图库中挑选图像,但是当我选择图像时,应用程序崩溃并且出现以下错误.. I/Process (31766):发送信号。PID:31766 SIG:9 失去与设备的连接。退出(sigterm)

这是我从图库中选择图像的图像选择器代码,mainfest 文件是 decribr brlow

import 'dart:io';

import 'package:flutter/material.dart';

class Crop extends StatefulWidget {
final Color color;
final File image;

  const Crop({Key key, this.color,this.image}) : super(key: key);

  @override
  _CropState createState() => _CropState(color,image);
}

class _CropState extends State<Crop> {
 Color color;
 File image;

  _CropState(this.color,this.image);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
         leading: IconButton(icon: Icon(Icons.done), onPressed: (){}),
         actions: [IconButton(icon: Icon(Icons.clear), onPressed: (){})],
        title: Center(
          child: Padding(
            padding: const EdgeInsets.only(right:10.0),
            
            child: Text('CROP PHOTO',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 20,
                  fontWeight: FontWeight.bold,

                  shadows: [
                    Shadow(
                      blurRadius: 10.0,
                      color: Colors.blue,
                      offset: Offset(5.0, 5.0),
                    ),
                       
                  ],
                )),
          ),
        ),
  
        backgroundColor: Colors.cyan,
      ),
      
      bottomNavigationBar: BottomNavigationBar(
  type: BottomNavigationBarType.fixed,
  backgroundColor: Colors.cyan,
  selectedItemColor: Colors.white,
  unselectedItemColor: Colors.white.withOpacity(.60),
  selectedFontSize: 14,
  unselectedFontSize: 14,
  onTap: (value) { 
  },
  items: [
    BottomNavigationBarItem(
      title: Text('Crop',style: TextStyle(color: Colors.white),),
      icon: Icon(Icons.crop),
    ),
    BottomNavigationBarItem(
      title: Text('Edit Text'),
      icon: Icon(Icons.text_fields),
    ),
  ],
),
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        decoration: BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topRight,
              end: Alignment.bottomLeft,
              colors: [Colors.cyan[300], Colors.purple]),
        ),
child: Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    Container(
      height: MediaQuery.of(context).size.height/2,
      width: MediaQuery.of(context).size.width,
      color:color,
      child: Image.file(image,height:40,width:100),
    ),
  ],
),
     ),
    );
  }
}

这是我最强大的文件代码


    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bravemindstudio.invitation_card_maker">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->


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



    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="invitation_card_maker"
        android:requestLegacyExternalStorage="true"
        android:icon="@mipmap/ic_launcher">
        
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

         <activity
    android:name="com.yalantis.ucrop.UCropActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

标签: androidiosflutterdart

解决方案


有可用的图像选择器和图像裁剪器库,使这些任务变得容易 图像选择器

图像裁剪器

您可以调用此函数从图库中选择图像

  Future _scanBytesFromGallery() async {
PickedFile pickedFilefile = await ImagePicker().getImage(
    source: ImageSource.gallery,
    imageQuality: 50,
    maxWidth: 300,
    maxHeight: 300);
_cropImage(pickedFilefile.path);

}

 _cropImage(filePath) async {
File croppedImage = await ImageCropper.cropImage(
    sourcePath: filePath,
    aspectRatioPresets: [
      CropAspectRatioPreset.square,
      CropAspectRatioPreset.ratio3x2,
      CropAspectRatioPreset.original,
      CropAspectRatioPreset.ratio4x3,
      CropAspectRatioPreset.ratio16x9
    ],
    compressFormat: ImageCompressFormat.jpg,

    androidUiSettings: AndroidUiSettings(
      toolbarColor: Colors.blueAccent,
      toolbarWidgetColor: Colors.white,
      toolbarTitle: "CROP",
      statusBarColor: Colors.blueAccent[900],
      initAspectRatio: CropAspectRatioPreset.original,
      lockAspectRatio: false,
    ),
    iosUiSettings: IOSUiSettings(
      minimumAspectRatio: 1.0,
    ));
imageFile = croppedImage;
imgsize = await imageFile.length();
setState(() {});

}


推荐阅读