add cleanCache api

This commit is contained in:
Wenqi Li 2021-01-10 21:09:25 +08:00
parent 749188bb82
commit 5ddd31fed9
3 changed files with 41 additions and 5 deletions

View File

@ -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<T>(val handler: () -> T, val post: (result: T) -> Unit) : AsyncTask<Void, Void, T>() {

View File

@ -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())
}
}

View File

@ -113,4 +113,8 @@ class PhotoGallery {
}) as String;
return File(path);
}
static Future<void> cleanCache() async {
_channel.invokeMethod('cleanCache', {});
}
}