如何在Flutter中访问Android的Internal Storage

问题描述 投票:0回答:5

在 Android 中,我们使用

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

我知道还有其他方法可以做到这一点,或者我遗漏了一些东西。

android dart flutter storage dart-packages
5个回答
2
投票

来自 Flutter 来源:

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

2
投票

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

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

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

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

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

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


0
投票

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;

0
投票

(这个包对于最新版本的 Flutter 来说已经过时了)

使用此 dart 包: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'),
          ],
        ) ,
      ),
    );
  }
}

0
投票

就我而言,我使用了

flutter_donwloader:
并且我希望推出

使用

android_intent
flutter_downloader

安装 apk

通过检索这些应用程序的下载路径并避免显示我的

app-Store app
执行的后端进程。

下载时,我发现使用 adb 和 regex 必须满足某些条件,它们是:

storage/emulated/0/Download/Google\ Lens_1.8.190904059_Apkpure.apk /home/enokseth
.

Android 设备上的路径基本上采用以下格式:

storage/emulated/0/Download/Google Lens_1.8.190904059_Apkpure.apk

/
对应于:

我的 apk-tilte 中的空间

Google Lens_1.8.190904059_Apkpoore.apk

使用这个正则表达式只需

path
android_intent
,

您可以检索内部存储中没有太多颤振包的真实路径。

这是 Android 中树遍历的一个很好的实用工具。

© www.soinside.com 2019 - 2024. All rights reserved.