我有一个 Oracle 数据库,它在名为
FILE_FILE_DATA
的字段中存储 PDF、XLS、JPG 等二进制文件,其类型为 LONG RAW
我需要从 Oracle 获取此数据并显示在我的 Flutter 应用程序中。
我使用 NodeJS 作为服务器,它能够成功获取数据并通过 http 调用将其返回给 flutter。
代码和获取的数据如下所示:
final data1 = json.decode(value.body)['rtnData'] as List<dynamic>;
final data2 = json.decode(data1[0]);
data = data2[0]['FILE_FILE_DATA']['data'];
debugPrint('data: $data');
data
现在打印一大组数字,如下所示:
[80, 75, 3, 4, 20, 0, 6, 0, 8, 0, 0, 0, 32, 0, 96, 193, 88, 5, 195, 12, 0, 0, 52, 81, 0, 0, 24, 0, 0, 0, 120, 108, 47, 119, 111, 114, 107, 115, 104, 101, 101, 116, 115, 47, 115, 104, 101, 101, 116, 49, 46, 120, 109, 108, 141, 156, 91, 115, 226, 184, 22, 70, 223, 187, 234, 252, 7, 138, 151, 60, 29, 130, 109, 12, 132, 234, 100]
谁能告诉我如何将其转换为本地文件并使用 launchUrl() 显示?我已尝试以下操作,但在第一行出现异常“
type 'Null' is not a subtype of type 'int'
”
Uint8List fileFileData = Uint8List.fromList(data);
Directory tempDir = await getTemporaryDirectory();
String path = tempDir.path;
// Clean up the attachment name (remove spaces)
String attachmentName = basename(fileName).replaceAll(" ", "");
// Create a File instance for the output file
FILE.File file = FILE.File('$path/$attachmentName');
String localFileName = ('$path/$attachmentName');
debugPrint('localFileName=$localFileName');
if (!await file.exists()) {
await file.create();
}
file.writeAsBytesSync(fileFileData);
======更新============
第一行:
Uint8List fileFileData = Uint8List.fromList(data);
我得到这个例外:
type 'List<dynamic>' is not a subtype of type 'List<int>'
但是数据确实只包含整数。我什至尝试过仅使用数字手动分配数据,即 [1, 5, 6, 65,......]
经过一些研究,我发现我需要使用
cast<int>()
,它有效。上面评论中的一些用户也提出了同样的建议,对此表示赞赏。
我在下面发布最终解决方案,以防对其他人有帮助:
var imageData = data2[0]['FILE_FILE_DATA']['data'];
try {
final ints = imageData.cast<int>();
final bytes = Uint8List.fromList(ints);
Directory? tempDir = await getDownloadsDirectory();
String path = tempDir!.path;
// Create a File instance for the output file
FILE.File file = FILE.File('$path/$attachmentName');
await file.writeAsBytes(bytes);
String localFileName = ('$path/$attachmentName');
await OpenFile.open(localFileName);
} catch (e) {
debugPrint('----error--$e');
}
为了使用 Openfile.open(),您还需要在 AndroidManifest.xml 中添加以下内容
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>