Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 如何在Flatter中从内部和外部存储中获取所有PDF文件?_Dart_Flutter - Fatal编程技术网

Dart 如何在Flatter中从内部和外部存储中获取所有PDF文件?

Dart 如何在Flatter中从内部和外部存储中获取所有PDF文件?,dart,flutter,Dart,Flutter,我想显示内部和外部存储中的所有pdf文件,所以点击该特定文件后,我想在全屏对话框中打开该文件 因此,要做到这一点,您需要: 授予对包含PDF文件的目录中的外部存储的访问权限。让我们调用该文件夹/pdf 列出该目录的所有文件并向用户显示它们 使用可以可视化PDF的应用程序打开所选文件 为了做到我所认为的一切,我建议您使用这些颤振包: 使用path_provider,您可以获取Android设备的外部存储目录 Directory extDir = await getExternalSto

我想显示内部和外部存储中的所有pdf文件,所以点击该特定文件后,我想在全屏对话框中打开该文件

因此,要做到这一点,您需要:

  • 授予对包含PDF文件的目录中的外部存储的访问权限。让我们调用该文件夹
    /pdf
  • 列出该目录的所有文件并向用户显示它们
  • 使用可以可视化PDF的应用程序打开所选文件
为了做到我所认为的一切,我建议您使用这些颤振包:

使用path_provider,您可以获取Android设备的外部存储目录

Directory extDir = await getExternalStorageDirectory();
String pdfPath = extDir + "/pdf/";
为了访问外部存储,您需要在
ApplicationManifest.xml
中设置此权限请求:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
一旦获得外部存储访问权,我们将列出我们的PDF目录:

List<FileSystemEntity> _files;
_files = dir.listSync(recursive: true, followLinks: false);
当用户点击它们时,你必须用查看器打开它们

要做到这一点并不是一个简单的方法,因为使用Android我们需要构建一个ContentUri,并将此URI的访问权授予外部应用程序查看器

所以我们在Android中这样做,我们用它来调用Android本机代码

省道:

static const platform =
      const MethodChannel('it.versionestabile.flutterapp000001/pdfViewer');
var args = {'url': fileName};
          platform.invokeMethod('viewPdf', args);
public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "it.versionestabile.flutterapp000001/pdfViewer";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
            new MethodChannel.MethodCallHandler() {
              @Override
              public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                if (call.method.equals("viewPdf")) {
                  if (call.hasArgument("url")) {
                    String url = call.argument("url");
                    File file = new File(url);
                    //*
                    Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
                            BuildConfig.APPLICATION_ID + ".provider",
                            file);
                            //*/
                    Intent target = new Intent(Intent.ACTION_VIEW);
                    target.setDataAndType(photoURI,"application/pdf");
                    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    target.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(target);
                    result.success(null);
                  }
                } else {
                  result.notImplemented();
                }
              }
            });
  }
}
本机Java代码:

static const platform =
      const MethodChannel('it.versionestabile.flutterapp000001/pdfViewer');
var args = {'url': fileName};
          platform.invokeMethod('viewPdf', args);
public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "it.versionestabile.flutterapp000001/pdfViewer";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
            new MethodChannel.MethodCallHandler() {
              @Override
              public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                if (call.method.equals("viewPdf")) {
                  if (call.hasArgument("url")) {
                    String url = call.argument("url");
                    File file = new File(url);
                    //*
                    Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
                            BuildConfig.APPLICATION_ID + ".provider",
                            file);
                            //*/
                    Intent target = new Intent(Intent.ACTION_VIEW);
                    target.setDataAndType(photoURI,"application/pdf");
                    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    target.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(target);
                    result.success(null);
                  }
                } else {
                  result.notImplemented();
                }
              }
            });
  }
}
毕竟,我们可以在Android上查看我们的PDF列表

你有很多东西要学。我希望这对你来说是一个有用的游乐场

这是用于外部存储的,但您也可以获取内部和临时目录,其操作与此处类似

如果你想在iOS上做同样的事情,你也需要在iOS项目上创建相同的本机代码pdfViewer。要做到这一点,请始终参考。请记住,iOS设备上不存在外部存储。因此,您只能使用应用程序沙盒文档文件夹或临时文件夹


快乐编码。

因此,要做到这一点,您需要:

  • 授予对包含PDF文件的目录中的外部存储的访问权限。让我们调用该文件夹
    /pdf
  • 列出该目录的所有文件并向用户显示它们
  • 使用可以可视化PDF的应用程序打开所选文件
为了做到我所认为的一切,我建议您使用这些颤振包:

使用path_provider,您可以获取Android设备的外部存储目录

Directory extDir = await getExternalStorageDirectory();
String pdfPath = extDir + "/pdf/";
为了访问外部存储,您需要在
ApplicationManifest.xml
中设置此权限请求:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
一旦获得外部存储访问权,我们将列出我们的PDF目录:

List<FileSystemEntity> _files;
_files = dir.listSync(recursive: true, followLinks: false);
当用户点击它们时,你必须用查看器打开它们

要做到这一点并不是一个简单的方法,因为使用Android我们需要构建一个ContentUri,并将此URI的访问权授予外部应用程序查看器

所以我们在Android中这样做,我们用它来调用Android本机代码

省道:

static const platform =
      const MethodChannel('it.versionestabile.flutterapp000001/pdfViewer');
var args = {'url': fileName};
          platform.invokeMethod('viewPdf', args);
public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "it.versionestabile.flutterapp000001/pdfViewer";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
            new MethodChannel.MethodCallHandler() {
              @Override
              public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                if (call.method.equals("viewPdf")) {
                  if (call.hasArgument("url")) {
                    String url = call.argument("url");
                    File file = new File(url);
                    //*
                    Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
                            BuildConfig.APPLICATION_ID + ".provider",
                            file);
                            //*/
                    Intent target = new Intent(Intent.ACTION_VIEW);
                    target.setDataAndType(photoURI,"application/pdf");
                    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    target.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(target);
                    result.success(null);
                  }
                } else {
                  result.notImplemented();
                }
              }
            });
  }
}
本机Java代码:

static const platform =
      const MethodChannel('it.versionestabile.flutterapp000001/pdfViewer');
var args = {'url': fileName};
          platform.invokeMethod('viewPdf', args);
public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "it.versionestabile.flutterapp000001/pdfViewer";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
            new MethodChannel.MethodCallHandler() {
              @Override
              public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                if (call.method.equals("viewPdf")) {
                  if (call.hasArgument("url")) {
                    String url = call.argument("url");
                    File file = new File(url);
                    //*
                    Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
                            BuildConfig.APPLICATION_ID + ".provider",
                            file);
                            //*/
                    Intent target = new Intent(Intent.ACTION_VIEW);
                    target.setDataAndType(photoURI,"application/pdf");
                    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    target.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(target);
                    result.success(null);
                  }
                } else {
                  result.notImplemented();
                }
              }
            });
  }
}
毕竟,我们可以在Android上查看我们的PDF列表

你有很多东西要学。我希望这对你来说是一个有用的游乐场

这是用于外部存储的,但您也可以获取内部和临时目录,其操作与此处类似

如果你想在iOS上做同样的事情,你也需要在iOS项目上创建相同的本机代码pdfViewer。要做到这一点,请始终参考。请记住,iOS设备上不存在外部存储。因此,您只能使用应用程序沙盒文档文件夹或临时文件夹


快乐编码。

在Android和iOS上?你在使用“内部”和“外部”关键字,所以我猜你在考虑“Android平台”。您是否打算仅在Android Platorm上使用您的应用程序?在iOS上,您只能访问应用程序沙盒“内部”存储。那个么你们的具体情况是什么呢?目前只寻找Android平台,但若你们两个都有解决方案,那个就太好了!:)在安卓和iOS上?你在使用“内部”和“外部”关键字,所以我猜你在考虑“Android平台”。您是否打算仅在Android Platorm上使用您的应用程序?在iOS上,您只能访问应用程序沙盒“内部”存储。那个么你们的具体情况是什么呢?目前只寻找Android平台,但若你们两个都有解决方案,那个就太好了!:)