gpt4 book ai didi

android - 从 url 下载 PDF 文件并存储在内部存储中,然后尝试从任何 PDF 阅读器打开它,它显示的文件格式在 flutter 中不受支持

转载 作者:行者123 更新时间:2023-12-05 00:05:15 25 4
gpt4 key购买 nike

我试图在我的应用程序中打开我下载的 PDF 文件,然后它显示正常,但是当我从任何其他应用程序(如 PDF 阅读器)打开它时,它显示的文件格式不受支持。

  • PDF文件存放在这个目录:
    内部存储/MyApp/pdf
  • 我想从任何 PDF 阅读器访问该 PDF 文件或从我的文件管理器打开它,所以需要做什么。
  • 我没有使用 getExternalStorageDirectory() 的路径目录
  • 我直接给内部存储路径,比如/storage/emulated/0/MyApp

这是我的完整代码

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;

void main(){
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: new FileDownloadView()

));
}


class FileDownloadView extends StatefulWidget {
@override
_FileDownloadViewState createState() => _FileDownloadViewState();
}

class _FileDownloadViewState extends State<FileDownloadView> {
String _filePath = "";

Future<String> get _localDevicePath async {

final _devicePath = await getExternalStorageDirectory();
return _devicePath.path;
}

Future<File> _localFile({String path, String type}) async {
String _path = await _localDevicePath;

var _newPath = await Directory("/storage/emulated/0/$path").create();
return File("${_newPath.path}/pdf.$type");
}

Future _downloadSamplePDF() async {
final _response =
await http.get("https://firebasestorage.googleapis.com/v0/b/text-recognition-28371.appspot.com/o/Direct%20Recruitment%202020.pdf?alt=media&token=114498c1-0e84-48e0-bda0-15d4b688f745");
if (_response.statusCode == 200) {
final _file = await _localFile(path: "MyApp", type: "pdf");
final _saveFile = await _file.writeAsBytes(_response.bodyBytes);

Logger().i("File write complete. File Path ${_saveFile.path}");
setState(() {
_filePath = _saveFile.path;
});
} else {
Logger().e(_response.statusCode);
}
}

Future _downloadSampleVideo() async {
final _response = await http.get(
"https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_2mb.mp4");
if (_response.statusCode == 200) {
final _file = await _localFile(type: "mp4", path: "videos");
final _saveFile = await _file.writeAsBytes(_response.bodyBytes);
Logger().i("File write complete. File Path ${_saveFile.path}");
setState(() {
_filePath = _saveFile.path;
});
} else {
Logger().e(_response.statusCode);
}
}


@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton.icon(
icon: Icon(Icons.file_download),
label: Text("Sample Pdf"),
onPressed: () {
_downloadSamplePDF();



},
),
FlatButton.icon(
icon: Icon(Icons.file_download),
label: Text("Sample Videos"),
onPressed: () {
_downloadSampleVideo();

},
),
Text(_filePath),
FlatButton.icon(
icon: Icon(Icons.shop_two),
label: Text("Show"),
onPressed: () async {
final _openFile = await OpenFile.open(_filePath);
Logger().i(_openFile);
},
),
],
),
),
);
}
}

Android list

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example2.downloder_app">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases, you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="downloder_app"
android:icon="@mipmap/ic_launcher"
android:testOnly="false">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>

最佳答案

您可能需要添加 permission_handler在您的工作流程中启用对设备存储的写入权限。

您是否能够验证下载的文件是否确实写入了存储?如果文件已下载但有 0 个字节,则可能存在问题。

我建议避免硬编码根路径,因为它可能会根据您使用的平台版本而改变。相反,您可以考虑使用 getApplicationDocumentsDirectory() - 这应该将文件写入 app 目录。如果数据需要对用户可见,可以使用getExternalStorageDirectory()适用于 Android。

关于android - 从 url 下载 PDF 文件并存储在内部存储中,然后尝试从任何 PDF 阅读器打开它,它显示的文件格式在 flutter 中不受支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60260788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com