Cached Images doesn't exists after App re-start

130 views
Skip to first unread message

Marc Andrew

unread,
Jan 2, 2020, 4:46:27 AM1/2/20
to Flutter Development (flutter-dev)
Hi all,

I'm trying to cache an image from the image_picker plugin with path_provider for later and offline use.
The image is saved in the getApplicationDocumentsDirectory() and I also save the image path in the shared preferences.
So that I can find the image again. The caching works as long I'm not stopping the App. 
If I stop the App and start it again, Flutter is saying the file doesn't exist.
I'm wondering how long the image is cached in the getApplicationDocumentsDirectory and why it is removed?
Is there a better way to cache/store the image to the device for later use?
I've seen a Youtube video by converting the image in base64 and save the converted image as String in the shared preferences.
Is this the best solution? 
Below you can see my code


import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
SharedPreferences prefs;

File _image;
File _memoryImage;

Future _onPhotoLibraryTapped() async {
// Image Picker plugin
final File image = await ImagePicker.pickImage(
source: ImageSource.gallery, imageQuality: 100);

final Directory directory = await getApplicationDocumentsDirectory();
// Copy the image into the new directory
final File newImage = await image.copy('${directory.path}/profile_image.jpg');

// Save the image path in shared preferences
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('profile_image', newImage.path);

// Print the image path
print(newImage.path);

setState(() {
_image = newImage;
});
}

Future _loadImageFromMemory() async {
// Get the image path from shared preferences
SharedPreferences prefs = await SharedPreferences.getInstance();
String imagePath = prefs.getString('profile_image') ?? null;

if(imagePath != null) {
setState(() {
_memoryImage = File(imagePath);
});
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
onPressed: () {
_loadImageFromMemory();
},
),
IconButton(
icon: Icon(Icons.add),
onPressed: () {
_onPhotoLibraryTapped();
},
),
],
),
body: Column(
children: <Widget>[
Container(
child: _image == null ? Text('No image selected') : Image.file(_image),
),
Container(
child: _memoryImage == null ? Text('No image in the cache') : Image.file(_memoryImage),
),
],
),
);
}
}



Thank you and Happy New Year!
Marc

Andy Greenshaw

unread,
Jan 2, 2020, 4:50:51 AM1/2/20
to Marc Andrew, Flutter Development (flutter-dev)
Don’t save the image path. On each app restart, the “root” directory of your app changes (for security reasons). Just save the name and rebuild the path using getApplicationDocumentsDirectory() each app start.

 

From: flutt...@googlegroups.com on behalf of Marc Andrew <marc4...@gmail.com>
Sent: Thursday, January 2, 2020 9:46 am
To: Flutter Development (flutter-dev)
Subject: Cached Images doesn't exists after App re-start
 
--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/44f97867-e98b-424c-9a91-528a1616b645%40googlegroups.com.

Marc Andrew

unread,
Jan 2, 2020, 6:29:27 AM1/2/20
to Flutter Development (flutter-dev)
Thank you Andy, I didn't know the "root" directory changes on each app restart.


Thanks,
Marc

To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages