removeFile do not remove my file
🐛 Bug Report
I am trying to remove a specific file from the cache and clear cache too.
Expected behavior
(The problem only occurs in Android)
My customer take a picture with the application and he can retry to take another one if the first is not good, so I want to remove the previous picture from the cache folder of the application : /data/user/0/package_name/cache/customCache/file_name
But nothing happens when I call the function from my CustomCacheManager class.
Reproduction steps
This is my CustomCacheManager like your example :
class CustomCacheManager extends BaseCacheManager {
static const key = "customCache";
static CustomCacheManager _instance;
factory CustomCacheManager() {
if (_instance == null) {
_instance = new CustomCacheManager._();
}
return _instance;
}
CustomCacheManager._() : super(key,
maxAgeCacheObject: Duration(days: 2),
maxNrOfCacheObjects: 20);
@override
Future<String> getFilePath() async {
var directory = await getTemporaryDirectory();
return join(directory.path, key);
}
Future<void> removePicture(String file) async {
await removeFile(file);
}
Future<void> clearCustomCache() async {
await emptyCache();
}
}
I am trying to remove the picture like that :
retryTakePicture(String imageToRemove) async {
await _customCacheManager.removePicture(imageToRemove).then((_) {
***some code***
});
}
So when I retry to take the picture, the file is not removed from the cache so I got CameraException :
CameraException(fileExists, File at path /data/user/0/*package_name*/cache/customCache/*file_name*' already exists. Cannot overwrite.)
I don't know if I misunderstood the purpose of the removeFile method or if it's a bug.
And I have the same problem with emptyCache().
Configuration
Version: 1.2.2
Platform: No problem on iOS
I just had a look at the code, and it looks like the part where the file is really deleted is not awaited. Do you directly try to save after the .removePicture function or does the user still has to take the image?
No, in removePicture() promise I show my cameraCapture screen and then when the user take the picture I recreated the path where the picture need to be saved with getFilePath() :
takePicture() async {
String path;
try {
String pictureFacePath = **file_name**;
path = join(await _customCacheManager.getFilePath(), pictureFacePath);
await _cameraController.takePicture(path);
}
...
} on CameraException catch (e) {
print(e);
}
}
When does the cameracontroller check if the file exists? If it does that before taking the picture that makes sense.
CameraController does the check when the user clicks on takePicture(), then the exception is throw in the takePicture method in Camera.java file :
public void takePicture(String filePath, @NonNull final Result result) {
final File file = new File(filePath);
if (file.exists()) {
result.error(
"fileExists", "File at path '" + filePath + "' already exists. Cannot overwrite.", null);
return;
}
[...]
}
Then it makes sense, can you try to wait for example 500ms and see if that works? (Isn't a final solution, just a check).
So I tried with a Future.delayed before switching to my camera capture view :
retryTakePicture(String imageToRemove) async {
await _customCacheManager.removePicture(imageToRemove);
await Future.delayed(Duration(milliseconds: 500), () {
***switching to camera capture view***
});
}
But unfortunately, this isn't working better.