无法加载资产:“/storage/emulated/0/Android/data/com.example.projectdesign_app/files/null/”

3bygqnnd  于 2023-04-18  发布在  Android
关注(0)|答案(1)|浏览(176)

如何访问应用程序中图像的文件路径,比方说我上传图像到应用程序,并希望使用相同的图像进行编辑,它得到这个错误,如何解决它?这是我的代码后,从相机或画廊选择一个图像,我声明了两个变量显示和一个应该编辑图像

ElevatedButton(
               onPressed:  ()async {
               // call dialog and get value "camera" or "galery"
               final type = await  _settingModalBottomSheet(context);
               if(type != null){
               // call image pikcer
              final pickedFile = await _openImagePicker(type);
                if(pickedFile !=null){ 
                  final directory = (await getExternalStorageDirectory())!.path;
                 await Directory('$directory/$_image').create(recursive: true);
                 final fullPath =
                 '$directory/$_image/${DateTime.now().millisecondsSinceEpoch}.png';
                  
                   // set here
                   setState(()
                     {
                      _image = pickedFile;  
                      
                      // Uint8List bytes = pickedFile.readAsBytesSync();
                      //  var simage = ImageProvider;
                      //     image= simage; 
                      // List<int> imageBase64 = _image!.readAsBytesSync();
                      // String imageAsString = base64Encode(imageBase64);
                      // Uint8List uint8list = base64.decode(imageAsString);
                      // image = Image.memory(uint8list);   

                      
                      image = fullPath;
                     });
                 }
                }
              },
               child: const Text('Select An Image'),
               )

这是我的代码编辑油漆图像

class ImagePainterExample extends StatefulWidget {
  const ImagePainterExample({super.key, required this.image});
  final String? image;

  @override
  // ignore: library_private_types_in_public_api
  _ImagePainterExampleState createState() => _ImagePainterExampleState(image_1: image);
}

class _ImagePainterExampleState extends State<ImagePainterExample> {
  final _imageKey = GlobalKey<ImagePainterState>();
  final _key = GlobalKey<ScaffoldState>();
   _ImagePainterExampleState({required this.image_1});
   String? image_1;

    

  // void saveImage() async {
  //   final image = await _imageKey.currentState?.exportImage();
  //   final directory = (await getApplicationDocumentsDirectory()).path;
  //   await Directory('$directory/sample').create(recursive: true);
  //   final fullPath =
  //       '$directory/sample/${DateTime.now().millisecondsSinceEpoch}.png';
  //   final imgFile = File(fullPath);
  //   imgFile.writeAsBytesSync(image!);
  //   await GallerySaver.saveImage(fullPath, toDcim: true);
  //   // ignore: use_build_context_synchronously
  //   ScaffoldMessenger.of(context).showSnackBar(
  //     SnackBar(
  //       backgroundColor: Colors.grey[700],
  //       padding: const EdgeInsets.only(left: 10),
  //       content: Row(
  //         mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //         children: [
  //           const Text("Image Exported successfully.",
  //               style: TextStyle(color: Colors.white)),
  //           TextButton(
  //             onPressed: () => OpenFile.open(fullPath),
  //             child: Text(
  //               "Open",
  //               style: TextStyle(
  //                 color: Colors.blue[200],
  //               ),
  //             ),
  //           )
  //         ],
  //       ),
  //     ),
  //   );
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _key,
      appBar: AppBar(
        title: const Text("Image Painter Example"),
        // actions: [
        //   IconButton(
        //     icon: const Icon(Icons.save_alt),
        //     onPressed: saveImage,
        //   )
        // ],
      ),
      body: ImagePainter.asset(
      image_1!,
        key: _imageKey,
        scalable: true,
        initialStrokeWidth: 2,
        initialColor: Colors.green,
        initialPaintMode: PaintMode.line,
        placeholderWidget: Text (image_1!),
      ),
    );
  }
  
 
  
  
}

如何解决这个问题?

ohtdti5x

ohtdti5x1#

问题是您正在尝试使用ImagePainter.asset构造函数,该构造函数旨在从assets文件夹加载图像。由于您希望从设备的存储中加载图像,因此应该使用ImagePainter.file构造函数。
首先,更改ElevatedButton的onPressed函数,将拾取的文件保存到fullPath:

if (pickedFile != null) {
  final directory = (await getExternalStorageDirectory())!.path;
  await Directory('$directory/$_image').create(recursive: true);
  final fullPath =
      '$directory/$_image/${DateTime.now().millisecondsSinceEpoch}.png';

  // Save picked file to fullPath
  final savedFile = File(fullPath);
  await savedFile.writeAsBytes(await pickedFile.readAsBytes());

  // set here
  setState(() {
    _image = pickedFile;
    image = fullPath;
  });
}

现在,更新ImagePainterExample小部件以使用ImagePainter.file构造函数:

body: ImagePainter.file(
  File(image_1!),
  key: _imageKey,
  scalable: true,
  initialStrokeWidth: 2,
  initialColor: Colors.green,
  initialPaintMode: PaintMode.line,
),

这应该可以解决问题并正确显示设备存储中的选定图像。

相关问题