How to get Internal and External (SD Card) Storage Root Path, App Dir, Available Space in Flutter App
In this example, we are going to show you the easiest way to get internal and external storage path in Flutter app. You will learn to get root path, app file directory, available free space in GB at both internal and external SD card storage in Flutter App. See the exampel below for more details.
First, you need to add path_provider_ex Flutter Package in your project by adding following lines in pubspect.yaml file.
dependencies:
flutter:
sdk: flutter
path_provider_ex: ^1.0.1
Add read / write permissions in your android/app/src/main/AndroidManifest.xml before <application> tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
If you still get the Permission Denied error, add the following line on AndroidManifest.xml file.
<application android:requestLegacyExternalStorage="true"
Import path_provider_ex package in your script:
import 'package:path_provider_ex/path_provider_ex.dart';
Get list of Storage avialable in device:
List<StorageInfo> storageInfo = await PathProviderEx.getStorageInfo();
You will get list of Storage Info where storageInfo[0] is for Internal storage and storageInfo[1] is for external SD card. You can check length of storageInfo variable to check if Internal storage or External storage are available. See the example below for more details.
if(storageInfo.length > 0){
//storageInfo[0] = Internal Storage
print("Root Dir:" + storageInfo[0].rootDir); //Internal Root Dir
print("App Files Dir:" + storageInfo[0].appFilesDir); //Internal App Dir
print("Available Spacke:" + storageInfo[0].availableGB.toString() + "GB"); //avaialble space
}else{
print("No any internal storage Availalbe.");
}
if(storageInfo.length > 1){
//storageInfo[1] = SD card
print("Root Dir:" + storageInfo[1].rootDir); //Internal Root Dir
print("App Files Dir:" + storageInfo[1].appFilesDir); //Internal App Dir
print("Available Spacke:" + storageInfo[1].availableGB.toString() + "GB"); //avaialble space
}else{
print("No any External storage Availalbe.");
}
Read This Also: How to list files/folder from internal or SD card storage in Flutter App
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider_ex/path_provider_ex.dart';
void main() {
runApp(
MaterialApp(
home: MyApp(),
)
);
}
class MyApp extends StatefulWidget{
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<StorageInfo> storageInfo = [];
@override
void initState() {
Future.delayed(Duration.zero, () async {
try {
storageInfo = await PathProviderEx.getStorageInfo();
/*
storageInfo[0] => Internal Storeage
storageInfo[1] => SD Card Storage
*/
setState(() {}); //update UI
} on PlatformException {
print("Error while getting paths.");
}
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text("Get Internal And SD Card Path"),
backgroundColor: Colors.redAccent,
),
body: Padding(
padding: EdgeInsets.all(20),
child:Column(
children: <Widget>[
Text( 'Internal Storage root: ${(storageInfo.length > 0) ? storageInfo[0].rootDir : "unavailable"}\n'),
Text('Internal Storage appFilesDir:${(storageInfo.length > 0) ? storageInfo[0].appFilesDir : "unavailable"}\n'),
Text('Internal Storage AvailableGB: ${(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: ${(storageInfo.length > 1) ? storageInfo[1].availableGB : "unavailable"}\n'),
]
)
)
);
}
}
In this way, you can get Internal and External SD card storeage info like path, app dir, root dir, availabel space.
Please Wait...
No any Comments on this Article