Convert base64 to image and save it in temp folder flutter

1,900
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)
Share:
1,900
BeHappy
Author by

BeHappy

Trying to be a good TS Developer

Updated on December 20, 2022

Comments

  • BeHappy
    BeHappy over 1 year

    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?