我正在用本机代码对RGB数据进行JPEG编码,而最终的JPEG编码数据在本机字节数组中可用。将数据写入磁盘的有效方法是什么?在此输出上不再需要进行任何处理。
我可能想到的一些选择是:
我有一种很强烈的感觉,即我的某些假设是错误的,请为我的学习大声疾呼。
如果文件仅由您的应用程序使用,则可以只写到应用程序缓存(getExternalCacheDir
)或数据(getExternalFilesDir
)目录。
如果要使它们可被其他文件访问,则可以按照提示使用MediaStore框架。您可以将官方文档中的this example修改为以下内容:
val resolver = applicationContext.contentResolver
val imagesCollection = MediaStore.Images.Media
.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val imageDetails = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, "fancy_image.jpg")
put(MediaStore.Images.Media.IS_PENDING, 1)
}
val imageContentUri = resolver.insert(imagesCollection, imageDetails)
resolver.openFileDescriptor(imageContentUri, "w", null).use { pfd ->
int fd = pfd.getFd
// this would be your native implementation. it should `write()` to the fd or
// call `fdopen` and then `fwrite`. The `use` block will automatically call
// `close` for you.
native_writeFile(fd)
}
// Now that we're finished, release the "pending" status, and allow other apps
// to see the image
imageDetails.clear()
imageDetails.put(MediaStore.Images.Media.IS_PENDING, 0)
resolver.update(imageContentUri, imageDetails, null, null)