0

I am using latest version of image_pickerplugin, including Flutter 2.0.3 version and also this is failing on a real device(Xiaomi Redmi Note 7).The only message I can see is Lost connection to device when I take the camera. Gallery works fine.This is my code.

import 'dart:io';

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

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

class _MyHomePageState extends State<MyHomePage> {
  File _image;
  final picker = ImagePicker();

  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.camera);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
      } else {
        print('No image selected.');
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Image Picker Example'),
      ),
      body: Center(
        child: _image == null
            ? Text('No image selected.')
            : Image.file(_image),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: getImage,
        tooltip: 'Pick Image',
        child: Icon(Icons.add_a_photo),
      ),
    );
  }
}

I already added all permissions.How can solve this problem?

Brutal
  • 798
  • 1
  • 14
  • 37
  • could you provide the logs – Jitesh Mohite Mar 30 '21 at 11:49
  • @JiteshMohite As I said only message I can see is Lost connection to device. If you meant Logcat How can I provide infinite messages here – Brutal Mar 30 '21 at 11:55
  • if you are runing android device, then see android logcats, you will get something there – Jitesh Mohite Mar 30 '21 at 11:57
  • `2021-03-30 17:00:29.900 29803-29803/com.example.flutter_app_image_save E/libc: Access denied finding property "ro.vendor.df.effect.conflict" 2021-03-30 17:00:29.966 29803-29861/com.example.flutter_app_image_save E/Perf: Fail to get file list com.example.flutter_app_image_save 2021-03-30 17:00:29.966 29803-29861/com.example.flutter_app_image_save E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array` @JiteshMohite there were errors like this when I try to take a photo – Brutal Mar 30 '21 at 12:02

1 Answers1

1

Add to the menifest android:requestLegacyExternalStorage="true"

android {
    compileSdkVersion 29
// ....
        defaultConfig {
            targetSdkVersion 29

This should work for you, if want to know more about then click android:requestLegacyExternalStorage

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147