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

Commit

Permalink
Add method for external storage
Browse files Browse the repository at this point in the history
Android supports accessing the "external" storage medium. Support
finding this path in flutter with a method call in path_provider
  • Loading branch information
natelust committed May 16, 2017
1 parent 6d8e70c commit 487ce83
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/path-provider/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.2.1] - 2017-05-16

* Add function to determine external storage directory.

## [0.2.0] - 2017-05-10

* Upgrade to new plugin registration. (https://groups.google.com/forum/#!topic/flutter-dev/zba1Ynf2OKM)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package io.flutter.plugins.path_provider;

import android.app.Activity;
import android.os.Environment;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
Expand Down Expand Up @@ -35,6 +36,9 @@ public void onMethodCall(MethodCall call, Result result) {
case "getApplicationDocumentsDirectory":
result.success(getPathProviderApplicationDocumentsDirectory());
break;
case "getStorageDirectory":
result.success(getPathProviderStorageDirectory());
break;
default:
result.notImplemented();
}
Expand All @@ -47,4 +51,8 @@ private String getPathProviderTemporaryDirectory() {
private String getPathProviderApplicationDocumentsDirectory() {
return PathUtils.getDataDirectory(activity);
}

private String getPathProviderStorageDirectory() {
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
}
26 changes: 26 additions & 0 deletions packages/path-provider/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
Future<Directory> _tempDirectory;
Future<Directory> _appDocumentsDirectory;
Future<Directory> _externalDocumentsDirectory;

void _requestTempDirectory() {
setState(() {
Expand Down Expand Up @@ -65,6 +66,12 @@ class _MyHomePageState extends State<MyHomePage> {
});
}

void _requestExternalStorageDirectory() {
setState(() {
_externalDocumentsDirectory = getExternalStorageDirectory();
});
}

@override
Widget build(BuildContext context) {
return new Scaffold(
Expand Down Expand Up @@ -105,6 +112,25 @@ class _MyHomePageState extends State<MyHomePage> {
child: new FutureBuilder<Directory>(
future: _appDocumentsDirectory, builder: _buildDirectory),
),
new Column(
children : <Widget>[
new Padding(
padding: const EdgeInsets.all(16.0),
child: new RaisedButton(
child: new Text('${Platform.isIOS ?
"External directories are unavailable " +
"on iOS":
"Get External Storage Directory" }'),
onPressed: Platform.isIOS ? null :
_requestExternalStorageDirectory,
),
),
]
),
new Expanded(
child: new FutureBuilder<Directory>(
future: _externalDocumentsDirectory, builder: _buildDirectory),
),
],
),
),
Expand Down
17 changes: 17 additions & 0 deletions packages/path-provider/lib/path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,20 @@ const _channel = const MethodChannel('plugins.flutter.io/path_provider');
return null;
return new Directory(path);
}

/// Path to a directory where the application may access top level storage.
/// The current operating system should be determined before issuing this
/// function call, as this functionality is only available on Android.
///
/// On iOS, this function throws an UnsupportedError as it is not possible
/// to access outside the app's sandbox.
///
/// On Android this returns getExternalStorageDirectory.
Future<Directory> getExternalStorageDirectory() async {
if (Platform.isIOS)
throw new UnsupportedError("Functionality not available on iOS");
final String path = await _channel.invokeMethod('getStorageDirectory');
if (path == null)
return null;
return new Directory(path);
}
2 changes: 1 addition & 1 deletion packages/path-provider/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: path_provider

version: 0.2.0
version: 0.2.1
description: A Flutter plugin for getting commonly used locations on the filesystem.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider
Expand Down

0 comments on commit 487ce83

Please sign in to comment.