我已经在我的应用程序中添加了对存储jpg和png的支持,但我对如何使其适用于视频和gif文件感到有点困惑。我的应用是用dart/flutter编写的,但使用kotlin存储安卓10+文件,因为dart/flutter不支持它。在flutter方面,我使用http get请求获取文件,然后将响应正文作为字节数组发送到应用程序的kotlin部分。第一个函数接收字节数组,并将其转换为位图,然后使用第二个函数将位图写入磁盘。如何添加对视频和gif文件的支持?我正在编写的视频文件很可能是webm和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()
}
}发布于 2021-02-02 23:22:15
修复为
@Throws(IOException::class)
private fun writeImage(fileBytes: ByteArray, name: String, mediaType: String, fileExt: String) {
val fos: OutputStream?
val resolver = contentResolver
val contentValues = ContentValues()
val imageUri: Uri?
if(mediaType == "image"){
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/")
imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
} else {
contentValues.put(MediaStore.Video.Media.DISPLAY_NAME, "$name.$fileExt")
contentValues.put(MediaStore.Video.Media.MIME_TYPE, "$mediaType/$fileExt")
contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, Environment.DIRECTORY_MOVIES + "/LoliSnatcher/")
imageUri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues)
}
if (imageUri != null){
fos = resolver.openOutputStream(imageUri);
fos?.write(fileBytes);
Objects.requireNonNull(fos)?.close()
}
}https://stackoverflow.com/questions/66011465
复制相似问题