1.现在常常以插件话和组件化开发移动端,我们在使用原生访问flutter的Assets资源时常常可以这样写
val flutterAssetStream = assetManager.open("flutter_assets/assets/my_flutter_asset.png")
但是如果是跨插件的话可能就不行了,比如一个主项目可能引入了好几个问呢-----》使用 AssetFileDescriptor
//assetPath的路径如:assets/my_flutter_asset.png?package=app
private fun assetSourceToBitmap(context: Context, assetPath: String,flutterAssets: FlutterPlugin.FlutterAssets): Bitmap? { var source: BufferedSource? = null try { val uri = Uri.parse(assetPath) val packageName = uri.getQueryParameter("package") val subPath = if (packageName.isNullOrBlank()) { flutterAssets.getAssetFilePathBySubpath(uri.path.orEmpty()) } else { flutterAssets.getAssetFilePathBySubpath(uri.path.orEmpty(), packageName) } val internalSource = context.assets.openFd(subPath) source = internalSource.createInputStream().source().buffer() val array = source.readByteArray() return BitmapFactory.decodeByteArray(array,0,array.size) } catch (e: Exception) { println("logo-IOException") e.printStackTrace() byteArrayOf() } finally { source?.close() } return null }
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
flutterAssets= flutterPluginBinding.flutterAssets
}
附:参考 https://pub.dev/packages/fluwx 的分享图片功能
标签:插件,assets,val,flutter,source,flutterAssets,Flutter From: https://www.cnblogs.com/maowuge/p/18676392