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

Add method for external storage #28

Merged
merged 1 commit into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use case for this? The documentation for getExternalDirectory() says not to use this directly but instead use getExternalStoragePublicDirectory(String) .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use case I had in mind for this was to get the root of the available file system, such that a file browser could be created within an app. I wanted to create a toy audio book app that would allow the user to navigate to a directory and mark it as the location all audio books could be found under. The latter API call you bring up is not quite targeted for this use case, as it expects you to specify what type of media you are interested in (i.e. for finding the location to save pictures). In my case the user may have located their audio books at any place accessible on the external storage.

}
}
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