Hibok
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

86 行
2.2 KiB

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