5

I have base64 string of image like /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//....

What I want to do is to save this image in temp folder and use that file address for showing image in my app.

How can I do that?

BeHappy
  • 3,705
  • 5
  • 18
  • 59
  • This post might help you: https://stackoverflow.com/questions/58669785/how-to-save-image-from-base64-string-to-file-in-flutter – Raul Marquez May 25 '20 at 06:29

1 Answers1

12
import 'package:path_provider/path_provider.dart' as syspaths;

Decode your base64 string to bytes in memory.

Uint8List bytes = base64.decode(base64String);

Make a temporary directory and file on that directory

final appDir = await syspaths.getTemporaryDirectory();
File file = File('${appDir.path}/sth.jpg');

Write converted bytes on a file

await file.writeAsBytes(bytes)

then we can

Image.file(file);

OR ALTERNATIVELY

Decode your base64 string to bytes in memory.

Uint8List bytes = base64.decode(base64String);

then we can

Image.memory(bytes)
Dipesh KC
  • 2,273
  • 1
  • 15
  • 19