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

Commit 487ce83

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 6d8e70c commit 487ce83

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-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-16
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package io.flutter.plugins.path_provider;
66

77
import android.app.Activity;
8+
import android.os.Environment;
89
import io.flutter.plugin.common.MethodCall;
910
import io.flutter.plugin.common.MethodChannel;
1011
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
@@ -35,6 +36,9 @@ public void onMethodCall(MethodCall call, Result result) {
3536
case "getApplicationDocumentsDirectory":
3637
result.success(getPathProviderApplicationDocumentsDirectory());
3738
break;
39+
case "getStorageDirectory":
40+
result.success(getPathProviderStorageDirectory());
41+
break;
3842
default:
3943
result.notImplemented();
4044
}
@@ -47,4 +51,8 @@ private String getPathProviderTemporaryDirectory() {
4751
private String getPathProviderApplicationDocumentsDirectory() {
4852
return PathUtils.getDataDirectory(activity);
4953
}
54+
55+
private String getPathProviderStorageDirectory() {
56+
return Environment.getExternalStorageDirectory().getAbsolutePath();
57+
}
5058
}

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,12 @@ class _MyHomePageState extends State<MyHomePage> {
6566
});
6667
}
6768

69+
void _requestExternalStorageDirectory() {
70+
setState(() {
71+
_externalDocumentsDirectory = getExternalStorageDirectory();
72+
});
73+
}
74+
6875
@override
6976
Widget build(BuildContext context) {
7077
return new Scaffold(
@@ -105,6 +112,25 @@ class _MyHomePageState extends State<MyHomePage> {
105112
child: new FutureBuilder<Directory>(
106113
future: _appDocumentsDirectory, builder: _buildDirectory),
107114
),
115+
new Column(
116+
children : <Widget>[
117+
new Padding(
118+
padding: const EdgeInsets.all(16.0),
119+
child: new RaisedButton(
120+
child: new Text('${Platform.isIOS ?
121+
"External directories are unavailable " +
122+
"on iOS":
123+
"Get External Storage Directory" }'),
124+
onPressed: Platform.isIOS ? null :
125+
_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 UnsupportedError 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.isIOS)
54+
throw new UnsupportedError("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)