Hibok
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

87 lignes
2.2 KiB

  1. import 'dart:io';
  2. import 'dart:typed_data';
  3. import 'package:path/path.dart' as p;
  4. import 'package:path_provider/path_provider.dart';
  5. class FileCacheMgr {
  6. static const key = "libCachedImageData";
  7. static FileCacheMgr _instance;
  8. /// The DefaultCacheManager that can be easily used directly. The code of
  9. /// this implementation can be used as inspiration for more complex cache
  10. /// managers.
  11. factory FileCacheMgr() {
  12. if (_instance == null) {
  13. _instance = new FileCacheMgr._();
  14. }
  15. return _instance;
  16. }
  17. FileCacheMgr._();
  18. Future<String> getFilePath() async {
  19. var directory = await getTemporaryDirectory();
  20. return p.join(directory.path, key);
  21. }
  22. Future<String> genFilePath(String fileId) async {
  23. var filePath = p.join(await getFilePath(), fileId);
  24. var folder = new File(filePath).parent;
  25. if (!(await folder.exists())) {
  26. folder.createSync(recursive: true);
  27. }
  28. return filePath;
  29. }
  30. Future<File> writeFile(String fileId, fileBytes) async {
  31. var path = p.join(await getFilePath(), fileId);
  32. print('receive path : $path');
  33. var folder = new File(path).parent;
  34. if (!(await folder.exists())) {
  35. folder.createSync(recursive: true);
  36. }
  37. var file = await File(path).writeAsBytes(fileBytes);
  38. return file;
  39. }
  40. Future<File> getFile(String fileId) async {
  41. var filePath = p.join(await getFilePath(), fileId);
  42. var file = File(filePath);
  43. if (await file.exists()) {
  44. return file;
  45. }
  46. return null;
  47. }
  48. static String pathKey;
  49. static initPathKey()async{
  50. if(pathKey==null && Platform.isIOS){
  51. Directory dir = await getTemporaryDirectory();
  52. String path =dir.path;
  53. int start = path.indexOf('Application')+12;
  54. int end = start+36;
  55. pathKey = path.substring(start,end);
  56. }
  57. }
  58. ///给ios文件路径替换成正确的
  59. static replacePath(String filePath) {
  60. if(Platform.isIOS && filePath!=null){
  61. // print('filePath $filePath');
  62. int start = filePath.indexOf('Application')+12;
  63. int end = start+36;
  64. filePath = filePath.replaceRange(start, end, pathKey);
  65. // print('after : $gg');
  66. }
  67. return filePath;
  68. }
  69. }