Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 282d8d2

Browse files
committed
Add method for external storage
Android supports accessing the "external" storage medium. Support finding this path in flutter with a method call in path_provider
1 parent b878214 commit 282d8d2

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

packages/path-provider/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.2.1] - 2017-05-11
2+
3+
* Add function to determine external storage directory
4+
15
## [0.2.0] - 2017-05-10
26

37
* Upgrade to new plugin registration. (https://groups.google.com/forum/#!topic/flutter-dev/zba1Ynf2OKM)

packages/path-provider/android/src/main/java/io/flutter/plugins/path_provider/PathProviderPlugin.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import io.flutter.plugin.common.PluginRegistry.Registrar;
1414
import io.flutter.util.PathUtils;
1515

16+
import android.os.Environment;
17+
1618

1719
public class PathProviderPlugin implements MethodCallHandler {
1820
private final Activity activity;
@@ -38,6 +40,9 @@ public void onMethodCall(MethodCall call, Result result) {
3840
case "getApplicationDocumentsDirectory":
3941
result.success(getPathProviderApplicationDocumentsDirectory());
4042
break;
43+
case "getStorageDirectory":
44+
result.success(getPathProviderStorageDirectory());
45+
break;
4146
default:
4247
result.notImplemented();
4348
}
@@ -50,4 +55,8 @@ private String getPathProviderTemporaryDirectory() {
5055
private String getPathProviderApplicationDocumentsDirectory() {
5156
return PathUtils.getDataDirectory(activity);
5257
}
58+
59+
private String getPathProviderStorageDirectory() {
60+
return Environment.getExternalStorageDirectory().getAbsolutePath();
61+
}
5362
}

packages/path-provider/example/lib/main.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class MyHomePage extends StatefulWidget {
3636
class _MyHomePageState extends State<MyHomePage> {
3737
Future<Directory> _tempDirectory;
3838
Future<Directory> _appDocumentsDirectory;
39+
Future<Directory> _externalDocumentsDirectory;
3940

4041
void _requestTempDirectory() {
4142
setState(() {
@@ -65,6 +66,13 @@ class _MyHomePageState extends State<MyHomePage> {
6566
});
6667
}
6768

69+
void _requestExternalStorageDirectory() {
70+
setState(() {
71+
if (Platform.operatingSystem != "ios")
72+
_externalDocumentsDirectory = getExternalStorageDirectory();
73+
});
74+
}
75+
6876
@override
6977
Widget build(BuildContext context) {
7078
return new Scaffold(
@@ -105,6 +113,24 @@ class _MyHomePageState extends State<MyHomePage> {
105113
child: new FutureBuilder<Directory>(
106114
future: _appDocumentsDirectory, builder: _buildDirectory),
107115
),
116+
new Cloumn(
117+
children : <Widget>[
118+
new Padding(
119+
padding: const EdgeInserts.add(16.0),
120+
child: new RaisedButton(
121+
child: const Test('${Platform.operatingSystem != "ios" ?
122+
"Get External Storage Directory" :
123+
"External Directories are unavailable " +
124+
"on iOS"}'),
125+
onPressed: _requestExternalStorageDirectory,
126+
),
127+
),
128+
]
129+
),
130+
new Expanded(
131+
child: new FutureBuilder<Directory>(
132+
future: _externalDocumentsDirectory, builder: _buildDirectory),
133+
),
108134
],
109135
),
110136
),

packages/path-provider/lib/path_provider.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,20 @@ const _channel = const MethodChannel('plugins.flutter.io/path_provider');
4040
return null;
4141
return new Directory(path);
4242
}
43+
44+
/// Path to a directory where the application may access top level storage.
45+
/// The current operating system should be determined before issuing this
46+
/// function call, as this functionality is only available on Android.
47+
///
48+
/// On iOS, this function throws an UnimplementedError as it is not possible
49+
/// to access outside the app's sandbox.
50+
///
51+
/// On Android this returns getExternalStorageDirectory.
52+
Future<Directory> getExternalStorageDirectory() async {
53+
if (Platform.operatingSystem == "ios")
54+
throw new UnimplementedError("Functionality not available on iOS");
55+
final String path = await _channel.invokeMethod('getStorageDirectory');
56+
if (path == null)
57+
return null;
58+
return new Directory(path);
59+
}

packages/path-provider/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: path_provider
22

3-
version: 0.2.0
3+
version: 0.2.1
44
description: A Flutter plugin for getting commonly used locations on the filesystem.
55
author: Flutter Team <[email protected]>
66
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider

0 commit comments

Comments
 (0)