BeHappy
BeHappy

Reputation: 3978

Convert base64 to image and save it in temp folder flutter

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?

Upvotes: 6

Views: 6869

Answers (1)

Dipesh KC
Dipesh KC

Reputation: 2463

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)

Upvotes: 14

Related Questions