我.txt在 Flutter 项目的文件夹资产中有一个文件,当应用程序在设备上打开时,SQFlite会创建一个数据库,并且应该从该文件行读取以将它们插入到数据库中。我需要阅读.txt文件的每一行并将它们添加到List<String>这样的:
List<String> _list = await new File('$dir/assets/file.txt').readAsLines();
Run Code Online (Sandbox Code Playgroud)
我尝试使用rootBundle但我无法将结果转换为 aFile并尝试像这样直接打开文件:
String dir = (await getApplicationDocumentsDirectory()).path;
Run Code Online (Sandbox Code Playgroud)
我总是找不到文件并收到错误消息。
var byte = await rootBundle.load('assets/bot.txt'); // Can't convert it to file
String dir = (await getApplicationDocumentsDirectory()).path;
List<String> _list = await new File('$dir/assets/file.txt').readAsLines(); // Error
error FileSystemException: Cannot open file, path = '/data/user/0/com.example.animationexp/app_flutter/assets/file.txt' (OS Error: No such file or directory, errno = 2) during open, closing...
Run Code Online (Sandbox Code Playgroud)
有没有办法让我打开和阅读这个文件?
我正在使用ExoPlayer在线服务器播放视频。因此,为了更好地为重播视频保存更多的互联网或数据,我只是将所有视频缓存到Cache directory. 但问题的话说,过时 constructor 的SimpleCache。
看我的代码:
private var sDownloadCache: SimpleCache? = null
fun getInstance(mContext: Context): SimpleCache {
val cacheEvictor = LeastRecentlyUsedCacheEvictor((100 * 1024 * 1024).toLong())
if (sDownloadCache == null)
sDownloadCache = SimpleCache(File(mContext.getCacheDir(), "media"),
cacheEvictor)
return sDownloadCache!!
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,编译器只是警告我构造函数 SimpleCache(File!, CacheEvictor!) 已被弃用。在 Java 中已弃用。所以,在那之后我试图在文件中找到方法,现在SimpleCache.java有一种使用方法SimpleCache是..
SimpleCache(File cacheDir, CacheEvictor evictor, DatabaseProvider databaseProvider)
Run Code Online (Sandbox Code Playgroud)
它对我来说是新的。所以,我的问题是如何使用这个新的构造函数而不是旧的不推荐使用的构造函数?有办法通过DatabaseProvider吗?如果是,那么如何或从什么?
我正在使用ExoPlayer库:
implementation 'com.google.android.exoplayer:exoplayer:2.10.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.10.1'
Run Code Online (Sandbox Code Playgroud)
并且因为这个版本提供了易于创建ExoPlayerFactory instance和build ProgressiveMediaSource.