【问题标题】:How to access Internal Storage of Android in Flutter如何在 Flutter 中访问 Android 的内部存储
【发布时间】:2019-01-20 10:39:19
【问题描述】:

在android中,我们使用getFileDir()getCacheDir()来访问Internal Storage。我可以看到有一个我可以使用的path_provider 插件,但我只能找出getTemporaryDirectory(),它类似于android 的getCacheDir()。那么有没有其他方法可以做 getFileDir() 在 Android 中所做的事情。

我知道有没有其他方法可以做到这一点,或者我错过了什么。

【问题讨论】:

    标签: android dart flutter storage dart-packages


    【解决方案1】:

    来自 Flutter 来源:

      /// Examples:
      ///
      ///  * iOS: `NSDocumentsDirectory`
      ///  * Android: The AppData directory.
      static Future<Directory> getApplicationDocumentsDirectory() async {
        return new Directory((await _pathProviderProxy.ptr.applicationDocumentsDirectory()).path);
    

    【讨论】:

    • 啊哈哈好地方:D
    • @Mosbah 它与getFileDir 并不完全相同,因为它返回路径data/data/your_app_package/filesgetApplicationDocumentDirectory 返回路径data/data/your_app_package/flutter/app_flutter,尽管我们可以使用app_flutter 目录而不是files没有任何问题
    • @Mosbah 你能提供你的代码的完整例子吗?因为我试过你的代码,但我需要定义_pathProviderProxy
    【解决方案2】:

    使用这个飞镖包:https://pub.dev/packages/path_provider_ex#-example-tab-

    import 'package:flutter/material.dart';
    import 'dart:async';
    
    import 'package:flutter/services.dart';
    import 'package:path_provider_ex/path_provider_ex.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      List<StorageInfo> _storageInfo = [];
    
      @override
      void initState() {
        super.initState();
        initPlatformState();
      }
    
      // Platform messages are asynchronous, so we initialize in an async method.
      Future<void> initPlatformState() async {
        List<StorageInfo> storageInfo;
        // Platform messages may fail, so we use a try/catch PlatformException.
        try {
          storageInfo = await PathProviderEx.getStorageInfo();
        } on PlatformException {}
    
        // If the widget was removed from the tree while the asynchronous platform
        // message was in flight, we want to discard the reply rather than calling
        // setState to update our non-existent appearance.
        if (!mounted) return;
    
        setState(() {
          _storageInfo = storageInfo;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Plugin example app'),
            ),
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                    'Internal Storage root:\n ${(_storageInfo.length > 0) ? _storageInfo[0].rootDir : "unavailable"}\n'),
                Text(
                    'Internal Storage appFilesDir:\n ${(_storageInfo.length > 0) ? _storageInfo[0].appFilesDir : "unavailable"}\n'),
                Text(
                    'Internal Storage AvailableGB:\n ${(_storageInfo.length > 0) ? _storageInfo[0].availableGB : "unavailable"}\n'),
                Text(
                    'SD Card root: ${(_storageInfo.length > 1) ? _storageInfo[1].rootDir : "unavailable"}\n'),
                Text(
                    'SD Card appFilesDir: ${(_storageInfo.length > 1) ? _storageInfo[1].appFilesDir : "unavailable"}\n'),
                Text(
                    'SD Card AvailableGB:\n ${(_storageInfo.length > 1) ? _storageInfo[1].availableGB : "unavailable"}\n'),
              ],
            ) ,
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案3】:

      path_provider 添加到pubspec.yaml

      import 'package:path_provider/path_provider.dart';
      
      Future<String> get _localPath async {
        final directory = await getApplicationDocumentsDirectory();
      
        return directory.path;
      }
      
      final directory = await _localPath;
      

      【讨论】:

        【解决方案4】:

        获取内部存储目录路径: 对于 Legacy 使用这些提供外部存储路径和外部公共存储路径的颤振插件, ext_storage: ^latest_ver

        String path = await ExtStorage.getExternalStoragePublicDirectory(
                ExtStorage.DIRECTORY_DOWNLOADS);
        

        详情:https://pub.dev/packages/ext_storage

        external_path:^latest_ver, 对于 NullSafety external_path 是一个flutter插件,提供内部、外部存储路径和外部公共存储路径,

        String path=await ExternalPath.getExternalStoragePublicDirectory(
                ExternalPath.DIRECTORY_DOWNLOADS);
        

        详情:https://pub.dev/packages/external_path

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-08
          相关资源
          最近更新 更多