How to access Internal Storage of Android in Flutter

17.9k Views Asked by At

In android we use getFileDir() and getCacheDir() for accessing the Internal Storage. I can see that there's a path_provider plugin that I can use but I can only figure out getTemporaryDirectory() which is analogous to getCacheDir() of android. So is there any alternative way of doing what getFileDir() does in Android.

Is there any other way to do this that I'm aware of, or I'm missing something.

4

There are 4 best solutions below

3
On BEST ANSWER

From Flutter sources:

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

Add path_provider to 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
On

(This package is outdated with latest version of Flutter)

Use this dart package: 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
On

To get internal storage directory path: For Legacy Use these flutter plugin that provides external storage path and external public storage path, ext_storage: ^latest_ver

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

Details:https://pub.dev/packages/ext_storage

external_path:^latest_ver, For NullSafety external_path is a flutter plugin that provides internal, external storage path and external public storage path,

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

Details:https://pub.dev/packages/external_path