[英]Unable to load asset in Flutter
我正在分享我之前使用image_picker
用相機拍攝的image_picker
。 它在模擬器上運行良好,但在我的設備上運行不正常:
onPressed: () async {
ByteData image = await rootBundle.load(team.avatar.path);
...
},
這是路徑的錯誤:
無法加載資源:/storage/emulated/0/Android/data/drodriguez.apps.Words/files/Pictures/scaled_2eccad3d-382e-462e-b124-b8fa06a2a32b791445736175256137.jpg
圖像顯示沒有錯誤,所以路徑是 100% 正確的:
Image.file(
_orderedTeams[index].avatar,
fit: BoxFit.cover,
height: 92.0,
width: 92.0,
)
我是否需要在pubspec.yml
上添加其他pubspec.yml
?
您不能使用rootBundle
訪問手機上的文件。 rootBundle(如其文檔中所述)僅適用於構建時與應用程序一起打包的文件(可能保存在資產上,在 pubspec 上聲明等)。
如果您想從手機加載圖像, 這可能會有所幫助。
這個函數可以讀取一個文件路徑並返回一個 Uint8List(一個 byteArray):
Future<Uint8List> _readFileByte(String filePath) async {
Uri myUri = Uri.parse(filePath);
File audioFile = new File.fromUri(myUri);
Uint8List bytes;
await audioFile.readAsBytes().then((value) {
bytes = Uint8List.fromList(value);
print('reading of bytes is completed');
}).catchError((onError) {
print('Exception Error while reading audio from path:' +
onError.toString());
});
return bytes;
}
而且,要獲得ByteData
只是:
var path = 'Some path to an image';
var byteArray = _readFileByte(path);
ByteData data = ByteData.view(byteArray.buffer);
(答案基於此)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.