firebase - 我的 Pdf 文档没有下载或打开,因为在颤动中下载后打开了图像文件
问题描述
我的 Pdf 文档没有下载或打开,因为在颤动中下载后打开了图像文件。
我已经使用文件选择器来选择图像文件和 pdf 文件并将它们发送到 firebase 存储。
我的图像文件正在下载和打开非常好,但在将 PDF 文件发送到 Firebase 存储后无法下载它。
可能是正在下载,但点击下载按钮时无法查看。
Future<void> downloadFile(StorageReference ref) async {
final String url = await ref.getDownloadURL();
final http.Response downloadData = await http.get(url);
final Directory systemTempDir = Directory.systemTemp;
final File tempFile = File('${systemTempDir.path}/tmp.jpg');
if (tempFile.existsSync()) {
await tempFile.delete();
}
await tempFile.create();
final StorageFileDownloadTask task = ref.writeToFile(tempFile);
final int byteCount = (await task.future).totalByteCount;
var bodyBytes = downloadData.bodyBytes;
final String name = await ref.getName();
final String path = await ref.getPath();
print(
'Success!\nDownloaded $name \nUrl: $url'
'\npath: $path \nBytes Count :: $byteCount',
);
_scaffoldKey.currentState.showSnackBar(
SnackBar(
backgroundColor: Colors.white,
content: Image.memory(
bodyBytes,
fit: BoxFit.fill,
),
),
);
}
}``
如果上述代码或任何地方需要任何更改,请建议我
解决方案
Flutter 默认支持打开图片,打开 pdf 需要使用 pdf 插件(依赖)。https://codecarbon.com/top-pdf-widgets-for-flutter/ 通过这个链接,我认为7. pdf_viewer_plugin适合你这个应用程序。
Dependencies:
flutter_full_pdf_viewer: ^1.0.6 # MIT Licence
创建一个新文件 display-pdf.dart 并在此处粘贴以下代码。
import 'package:flutter/material.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
class DisplayPDF extends StatelessWidget {
final String pdfPath;
final String title;
DisplayPDF(this.pdfPath, this.title);
@override
Widget build(BuildContext context) {
return PDFViewerScaffold(
appBar: AppBar(
title: Text(title),
centerTitle: true,
),
path: pdfPath);
}
}
创建一个文件 pdf-utilities.dart 并在此处粘贴以下代码。
import 'dart:async';
import 'dart:typed_data';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
class PdfUtilities {
final String pdfPath;
final BuildContext context;
PdfUtilities(this.pdfPath, this.context);
Future<String> prepareTestPdf() async {
final ByteData bytes = await DefaultAssetBundle.of(context).load(pdfPath);
final Uint8List list = bytes.buffer.asUint8List();
final tempDir = await getTemporaryDirectory();
final tempDocumentPath = '${tempDir.path}/$pdfPath';
final file = await File(tempDocumentPath).create(recursive: true);
file.writeAsBytesSync(list);
return tempDocumentPath;
}
}
替换Future<void> downloadFile(StorageReference ref) async
为以下代码:
Future<void> downloadFile(StorageReference ref) async {
final String url = await ref.getDownloadURL();
final http.Response downloadFile = await http.get(url);
final Directory systemTempDir = Directory.systemTemp;
final File tempFile = File('${systemTempDir.path}/tmp.pdf');
if (tempFile.existsSync()) {
await tempFile.delete();
}
await tempFile.create();
final StorageFileDownloadTask task = ref.writeToFile(tempFile);
final int byteCount = (await task.future).totalByteCount;
var bodyBytes = downloadFile.bodyBytes;
final String name = await ref.getName();
final String path = await ref.getPath();
print(
'Success!\nDownloaded $name \nUrl: $url'
'\npath: $path \nBytes Count :: $byteCount',
);
final String title = 'Displaying PDF';
PdfUtilities pdf = new PdfUtilities(path, context);
pdf.prepareTestPdf().then(writeCounter(await bodyBytes),
(path) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DisplayPDF(path, title),
),
);
},
);
}
Future<File> writeCounter(Uint8List stream) async {
final file = await _localFile;
// Write the file
return file.writeAsBytes(stream);
}
推荐阅读
- html - 如果未找到 ID,如何默认 SVG 精灵
- javascript - 使用 javascript UDF 在 BigQuery 中对整数数组的值求和
- python - 在 pandas 中按组连续获得最长连续周数
- jquery - 未捕获的类型错误:无法读取 null 的属性“按钮”
- zsh - ZSH“找不到命令:z”
- java - 如何让spring cron作业由spring boot的app用户以外的用户执行
- python - 如何从空文件初始化dict文件代码?
- python - 给定 http 链接的源代码中的 Grep 字符串
- python - CUDA_ERROR_OUT_OF_MEMORY 张量流
- json - Python 和 BeautifulSoup:如何将 JSON 转换为 CSV