diff --git a/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt b/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt index 110375d..2455480 100644 --- a/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt +++ b/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt @@ -10,6 +10,7 @@ import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.PluginRegistry.Registrar import android.graphics.Bitmap import java.io.ByteArrayOutputStream +import java.io.File import android.provider.MediaStore import android.content.Context import android.database.Cursor @@ -140,6 +141,10 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { result.success(v) }) } + "cleanCache" -> { + cleanCache() + result.success(null) + } else -> result.notImplemented() } } @@ -638,6 +643,21 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { "modifiedDate" to dateModified ) } + + private fun getCachePath(): File? { + return this.context?.run { + val cachePath = File(this.cacheDir, "photo_gallery") + if (!cachePath.exists()) { + cachePath.mkdirs() + } + return@run cachePath + } + } + + private fun cleanCache() { + val cachePath = getCachePath() + cachePath?.deleteRecursively() + } } class BackgroundAsyncTask(val handler: () -> T, val post: (result: T) -> Unit) : AsyncTask() { diff --git a/ios/Classes/SwiftPhotoGalleryPlugin.swift b/ios/Classes/SwiftPhotoGalleryPlugin.swift index 79a11b4..1b7a04c 100644 --- a/ios/Classes/SwiftPhotoGalleryPlugin.swift +++ b/ios/Classes/SwiftPhotoGalleryPlugin.swift @@ -75,6 +75,10 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { result(filepath?.replacingOccurrences(of: "file://", with: "")) }) } + else if(call.method == "cleanCache") { + cleanCache() + result(nil) + } else { result(FlutterMethodNotImplemented) } @@ -387,11 +391,8 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { let mediumId = asset.localIdentifier .replacingOccurrences(of: "/", with: "__") .replacingOccurrences(of: "\\", with: "__") - let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) - let tempFolder = paths[0].appendingPathComponent("photo_gallery") - try! FileManager.default.createDirectory(at: tempFolder, withIntermediateDirectories: true, attributes: nil) - - return tempFolder.appendingPathComponent(mediumId+ext) + let cachePath = self.cachePath() + return cachePath.appendingPathComponent(mediumId + ext) } private func toSwiftMediumType(value: String) -> PHAssetMediaType? { @@ -448,4 +449,15 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { let uti = self.extractUTIFromAsset(asset: asset) return self.extractFileExtensionFromUTI(uti: uti) } + + private func cachePath() -> URL { + let paths = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask) + let cacheFolder = paths[0].appendingPathComponent("photo_gallery") + try! FileManager.default.createDirectory(at: cacheFolder, withIntermediateDirectories: true, attributes: nil) + return cacheFolder + } + + private func cleanCache() { + try? FileManager.default.removeItem(at: self.cachePath()) + } } diff --git a/lib/photo_gallery.dart b/lib/photo_gallery.dart index abcc609..a7548cc 100644 --- a/lib/photo_gallery.dart +++ b/lib/photo_gallery.dart @@ -113,4 +113,8 @@ class PhotoGallery { }) as String; return File(path); } + + static Future cleanCache() async { + _channel.invokeMethod('cleanCache', {}); + } }