Commit 4eb8ee25 authored by zhouteng's avatar zhouteng

将android 7.0中FileProvider获取url的方法集成到插件库中

parent 409afdac
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zt.shareextend"> package="com.zt.shareextend">
<application>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.zt.shareextend.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_path" />
</provider>
</application>
</manifest> </manifest>
...@@ -15,9 +15,13 @@ import android.support.v4.content.FileProvider; ...@@ -15,9 +15,13 @@ import android.support.v4.content.FileProvider;
import java.io.File; import java.io.File;
import java.util.Map; import java.util.Map;
/** Plugin method host for presenting a share sheet via Intent */ /**
* Plugin method host for presenting a share sheet via Intent
*/
public class ShareExtendPlugin implements MethodChannel.MethodCallHandler { public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
private static final String authorities = "com.zt.shareextend.fileprovider";
private static final String CHANNEL = "share_extend"; private static final String CHANNEL = "share_extend";
public static void registerWith(Registrar registrar) { public static void registerWith(Registrar registrar) {
...@@ -39,14 +43,14 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler { ...@@ -39,14 +43,14 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
throw new IllegalArgumentException("Map argument expected"); throw new IllegalArgumentException("Map argument expected");
} }
// Android does not support showing the share sheet at a particular point on screen. // Android does not support showing the share sheet at a particular point on screen.
share((String) call.argument("text"),(String) call.argument("type"),(String) call.argument("authorities")); share((String) call.argument("text"), (String) call.argument("type"));
result.success(null); result.success(null);
} else { } else {
result.notImplemented(); result.notImplemented();
} }
} }
private void share(String text, String type,String authorities) { private void share(String text, String type) {
if (text == null || text.isEmpty()) { if (text == null || text.isEmpty()) {
throw new IllegalArgumentException("Non-empty text expected"); throw new IllegalArgumentException("Non-empty text expected");
} }
...@@ -59,14 +63,14 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler { ...@@ -59,14 +63,14 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
shareIntent.setType("text/plain"); shareIntent.setType("text/plain");
} else { } else {
File f = new File(text); File f = new File(text);
Uri uri = getUriForFile(mRegistrar.context(), authorities,f); Uri uri = getUriForFile(mRegistrar.context(), f);
if ("file".equals(type)) { if ("file".equals(type)) {
shareIntent.setType("application/*"); shareIntent.setType("application/*");
} }
if ("image".equals(type)) { if ("image".equals(type)) {
shareIntent.setType("image/*"); shareIntent.setType("image/*");
} }
shareIntent.putExtra(Intent.EXTRA_STREAM,uri); shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
} }
Intent chooserIntent = Intent.createChooser(shareIntent, null /* dialog title optional */); Intent chooserIntent = Intent.createChooser(shareIntent, null /* dialog title optional */);
...@@ -78,9 +82,10 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler { ...@@ -78,9 +82,10 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
} }
} }
public static Uri getUriForFile(Context context, String authories, File file) {
public static Uri getUriForFile(Context context, File file) {
if (Build.VERSION.SDK_INT >= 24) { if (Build.VERSION.SDK_INT >= 24) {
return FileProvider.getUriForFile(context, authories, file); return FileProvider.getUriForFile(context, authorities, file);
} else { } else {
return Uri.fromFile(file); return Uri.fromFile(file);
} }
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<paths> <paths>
<external-path <root-path
name="external_cache" name="root_path"
path="" /> path="/" />
</paths> </paths>
</resources> </resources>
\ No newline at end of file
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
to allow setting breakpoints, to provide hot reload, etc. to allow setting breakpoints, to provide hot reload, etc.
--> -->
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- io.flutter.app.FlutterApplication is an android.app.Application that <!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method. calls FlutterMain.startInitialization(this); in its onCreate method.
...@@ -36,15 +37,5 @@ ...@@ -36,15 +37,5 @@
</intent-filter> </intent-filter>
</activity> </activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.zt.shareextend.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_path" />
</provider>
</application> </application>
</manifest> </manifest>
...@@ -3,6 +3,8 @@ import 'dart:io'; ...@@ -3,6 +3,8 @@ import 'dart:io';
import 'package:share_extend/share_extend.dart'; import 'package:share_extend/share_extend.dart';
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
import 'package:permission/permission.dart';
import 'package:path_provider/path_provider.dart';
void main() => runApp(new MyApp()); void main() => runApp(new MyApp());
...@@ -12,9 +14,6 @@ class MyApp extends StatefulWidget { ...@@ -12,9 +14,6 @@ class MyApp extends StatefulWidget {
} }
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
///define in android project AndroidManifest.xml for FileProvider
static const String authorities = "com.zt.shareextend.fileprovider";
@override @override
void initState() { void initState() {
super.initState(); super.initState();
...@@ -32,7 +31,7 @@ class _MyAppState extends State<MyApp> { ...@@ -32,7 +31,7 @@ class _MyAppState extends State<MyApp> {
children: <Widget>[ children: <Widget>[
new RaisedButton( new RaisedButton(
onPressed: () { onPressed: () {
ShareExtend.share("share text", "text", authorities); ShareExtend.share("share text", "text");
}, },
child: new Text("share text"), child: new Text("share text"),
), ),
...@@ -40,7 +39,7 @@ class _MyAppState extends State<MyApp> { ...@@ -40,7 +39,7 @@ class _MyAppState extends State<MyApp> {
onPressed: () async { onPressed: () async {
File f = File f =
await ImagePicker.pickImage(source: ImageSource.gallery); await ImagePicker.pickImage(source: ImageSource.gallery);
ShareExtend.share(f.path, "image", authorities); ShareExtend.share(f.path, "image");
}, },
child: new Text("share image"), child: new Text("share image"),
), ),
...@@ -48,13 +47,62 @@ class _MyAppState extends State<MyApp> { ...@@ -48,13 +47,62 @@ class _MyAppState extends State<MyApp> {
onPressed: () async { onPressed: () async {
File f = File f =
await ImagePicker.pickImage(source: ImageSource.gallery); await ImagePicker.pickImage(source: ImageSource.gallery);
ShareExtend.share(f.path, "file", authorities); ShareExtend.share(f.path, "file");
}, },
child: new Text("share file"), child: new Text("share file"),
), ),
new RaisedButton(
onPressed: () {
if (Platform.isAndroid) {
_shareFileWithPerm();
}
},
child: new Text("share android external storage file"),
),
], ],
)), )),
), ),
); );
} }
/// share the external storage file ,first check the permission
_shareFileWithPerm() async {
if (await _checkPermission()) {
_shareFile();
} else {
var result = await _requestPermission();
/// to ask for permission
if (result == PermissionStatus.allow) {
_shareFile();
}
}
}
/// create a test file for the share example
_shareFile() async {
String filePath = await _createTestFileToShare();
ShareExtend.share(filePath, "file");
}
///create a test file to share
_createTestFileToShare() async {
Directory storageDir = await getExternalStorageDirectory();
File testFile = new File("${storageDir.path}/flutter/test.txt");
if (!await testFile.exists()) {
await testFile.create(recursive: true);
}
testFile.writeAsStringSync("test for share");
return testFile.path;
}
_checkPermission() async {
List<Permissions> perms =
await Permission.getPermissionStatus([PermissionName.Storage]);
return perms[0].permissionStatus == PermissionStatus.allow;
}
_requestPermission() async {
return await Permission.requestSinglePermission(PermissionName.Storage);
}
} }
...@@ -20,6 +20,8 @@ dependencies: ...@@ -20,6 +20,8 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2 cupertino_icons: ^0.1.2
image_picker: ^0.4.10 image_picker: ^0.4.10
path_provider: ^0.4.1
permission: ^0.1.0
dev_dependencies: dev_dependencies:
......
...@@ -18,14 +18,13 @@ class ShareExtend { ...@@ -18,14 +18,13 @@ class ShareExtend {
/// on iOS. /// on iOS.
/// type "text", "image" ,"file" /// type "text", "image" ,"file"
/// ///
static Future<void> share(String text, String type, String authorities, static Future<void> share(String text, String type,
{Rect sharePositionOrigin}) { {Rect sharePositionOrigin}) {
assert(text != null); assert(text != null);
assert(text.isNotEmpty); assert(text.isNotEmpty);
final Map<String, dynamic> params = <String, dynamic>{ final Map<String, dynamic> params = <String, dynamic>{
'text': text, 'text': text,
'type': type, 'type': type
'authorities': authorities
}; };
if (sharePositionOrigin != null) { if (sharePositionOrigin != null) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment