I have added support to my app for storing jpg and png but am a bit confused about how i'd get it to work for video and gif files. My app is written in dart/flutter but is using kotlin for the android 10+ file storage since dart/flutter has no support for it. On the flutter side I am getting the file with a http get request and then sending the response body to the kotlin part of the app as a byte array. The first function receives the byte array and converts it to a bitmap which is then written to disc using the second function. How would I add support for video and gif files? The video files I'm writing will most likely be webm and mp4
else if (call.method == "writeImage"){
var imageData = call.argument<ByteArray>("imageData");
val fileName = call.argument<String>("fileName");
val mediaType = call.argument<String>("mediaType");
val fileExt = call.argument<String>("fileExt");
var bmp = imageData?.size?.let { BitmapFactory.decodeByteArray(imageData,0, it) };
if (bmp != null && imageData != null && mediaType != null && fileExt != null && fileName != null){
print("writing file");
writeImage(bmp,fileName,mediaType,fileExt);
result.success(fileName);
} else {
print("a value is null");
result.success(null);
}
}
@Throws(IOException::class)
private fun writeImage(bitmap: Bitmap, name: String, mediaType: String, fileExt: String) {
val fos: OutputStream?
val resolver = contentResolver
val contentValues = ContentValues()
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "$name.$fileExt")
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "$mediaType/$fileExt")
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/LoliSnatcher/")
val imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
if (imageUri != null){
fos = resolver.openOutputStream(imageUri);
if (fileExt.toUpperCase() == "PNG"){
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} else if (fileExt.toUpperCase() == "JPG" || fileExt.toUpperCase() == "JPEG") {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
}
Objects.requireNonNull(fos)?.close()
}
}