-1

I am still struggiling with this annoying error. I have base64 string which I want to convert to Image. Here is the simpliest piece of code which is doing exactly what I want (at least, I saw it in different answers and code samples on the SO). I am getting the error:

Invalid character (at character 6)

my code is:

final String encodedStr = 'https://securelink.com/cameratypes/picture/13/true';
    Uint8List bytes = base64.decode(encodedStr);

and i want to disply image:

 Image.memory(bytes)
4

1 回答 1

0

最后,我找到了解决方案,我不知道它是否重要并且对像我这样挣扎的人有用,但我会提供帮助。所以,这将是简单快捷,因为我已经将我的图像转换为需要的格式(我的图像是 base64 格式),当我试图再次将它转换为字符串时,我犯了一个愚蠢的错误,因为它已经是一个字符串,我需要 Uint8List 格式。旁注:如果您的 api 开发人员说它应该采用 cookie 或任何类型的身份验证,则应该如此。

代码:

Future<String> _createFileFromString() async {
  final response = await http.get(
      Uri.parse(
        'your link here',
      ),
     headers: {
        'cookie':
            'your cookie here'
      });

  final Uint8List bytes = response.bodyBytes;
  String dir = (await getApplicationDocumentsDirectory()).path;
  String fullPath = '$dir/abc.png';
  print("local file full path ${fullPath}");
  File file = File(fullPath);
  await file.writeAsBytes(List.from(bytes));
  print(file.path);

  final result = await ImageGallerySaver.saveImage(bytes);
  print(result);

  return file.path;
}

此代码将您的图像直接保存到应用程序库中,并且不会在屏幕上显示任何内容

于 2022-02-15T13:15:15.717 回答