Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
ShareExtend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李增强
ShareExtend
Commits
a6165e59
Commit
a6165e59
authored
Dec 18, 2018
by
zhouteng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
将动态权限的检查封装到插件库中
parent
4eb8ee25
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
56 deletions
+60
-56
android/src/main/AndroidManifest.xml
android/src/main/AndroidManifest.xml
+2
-0
android/src/main/java/com/zt/shareextend/ShareExtendPlugin.java
...d/src/main/java/com/zt/shareextend/ShareExtendPlugin.java
+50
-5
example/lib/main.dart
example/lib/main.dart
+8
-49
example/pubspec.yaml
example/pubspec.yaml
+0
-2
No files found.
android/src/main/AndroidManifest.xml
View file @
a6165e59
<manifest
xmlns:android=
"http://schemas.android.com/apk/res/android"
package=
"com.zt.shareextend"
>
<uses-permission
android:name=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<application>
<provider
...
...
android/src/main/java/com/zt/shareextend/ShareExtendPlugin.java
View file @
a6165e59
...
...
@@ -2,13 +2,19 @@ package com.zt.shareextend;
import
io.flutter.plugin.common.MethodCall
;
import
io.flutter.plugin.common.MethodChannel
;
import
io.flutter.plugin.common.PluginRegistry
;
import
io.flutter.plugin.common.PluginRegistry.Registrar
;
import
android.Manifest
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.pm.PackageManager
;
import
android.net.Uri
;
import
android.os.Build
;
import
android.os.Environment
;
import
android.support.v4.app.ActivityCompat
;
import
android.support.v4.content.ContextCompat
;
import
android.support.v4.content.FileProvider
;
...
...
@@ -18,19 +24,24 @@ import java.util.Map;
/**
* Plugin method host for presenting a share sheet via Intent
*/
public
class
ShareExtendPlugin
implements
MethodChannel
.
MethodCallHandler
{
public
class
ShareExtendPlugin
implements
MethodChannel
.
MethodCallHandler
,
PluginRegistry
.
RequestPermissionsResultListener
{
/// the authorities for FileProvider
private
static
final
String
authorities
=
"com.zt.shareextend.fileprovider"
;
private
static
final
int
CODE_ASK_PERMISSION
=
100
;
private
static
final
String
CHANNEL
=
"share_extend"
;
private
final
Registrar
mRegistrar
;
private
String
text
;
private
String
type
;
public
static
void
registerWith
(
Registrar
registrar
)
{
MethodChannel
channel
=
new
MethodChannel
(
registrar
.
messenger
(),
CHANNEL
);
ShareExtendPlugin
instance
=
new
ShareExtendPlugin
(
registrar
);
final
ShareExtendPlugin
instance
=
new
ShareExtendPlugin
(
registrar
);
registrar
.
addRequestPermissionsResultListener
(
instance
);
channel
.
setMethodCallHandler
(
instance
);
}
private
final
Registrar
mRegistrar
;
private
ShareExtendPlugin
(
Registrar
registrar
)
{
this
.
mRegistrar
=
registrar
;
...
...
@@ -54,6 +65,8 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
if
(
text
==
null
||
text
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"Non-empty text expected"
);
}
this
.
text
=
text
;
this
.
type
=
type
;
Intent
shareIntent
=
new
Intent
();
shareIntent
.
setAction
(
Intent
.
ACTION_SEND
);
...
...
@@ -62,6 +75,14 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
shareIntent
.
putExtra
(
Intent
.
EXTRA_TEXT
,
text
);
shareIntent
.
setType
(
"text/plain"
);
}
else
{
if
(
isPathInExternalStorage
(
text
))
{
if
(!
checkPermisson
())
{
requestPermission
();
return
;
}
}
File
f
=
new
File
(
text
);
Uri
uri
=
getUriForFile
(
mRegistrar
.
context
(),
f
);
if
(
"file"
.
equals
(
type
))
{
...
...
@@ -82,8 +103,32 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
}
}
private
boolean
isPathInExternalStorage
(
String
path
)
{
File
storagePath
=
Environment
.
getExternalStorageDirectory
();
return
path
.
startsWith
(
storagePath
.
getAbsolutePath
());
}
private
boolean
checkPermisson
()
{
if
(
ContextCompat
.
checkSelfPermission
(
mRegistrar
.
context
(),
Manifest
.
permission
.
READ_EXTERNAL_STORAGE
)
==
PackageManager
.
PERMISSION_GRANTED
)
{
return
true
;
}
return
false
;
}
private
void
requestPermission
()
{
ActivityCompat
.
requestPermissions
(
mRegistrar
.
activity
(),
new
String
[]{
Manifest
.
permission
.
READ_EXTERNAL_STORAGE
},
CODE_ASK_PERMISSION
);
}
@Override
public
boolean
onRequestPermissionsResult
(
int
requestCode
,
String
[]
perms
,
int
[]
grantResults
)
{
if
(
requestCode
==
CODE_ASK_PERMISSION
&&
grantResults
.
length
>
0
&&
grantResults
[
0
]
==
PackageManager
.
PERMISSION_GRANTED
)
{
share
(
text
,
type
);
}
return
false
;
}
p
ublic
static
Uri
getUriForFile
(
Context
context
,
File
file
)
{
p
rivate
static
Uri
getUriForFile
(
Context
context
,
File
file
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
24
)
{
return
FileProvider
.
getUriForFile
(
context
,
authorities
,
file
);
}
else
{
...
...
example/lib/main.dart
View file @
a6165e59
...
...
@@ -3,7 +3,6 @@ import 'dart:io';
import
'package:share_extend/share_extend.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
());
...
...
@@ -43,21 +42,11 @@ class _MyAppState extends State<MyApp> {
},
child:
new
Text
(
"share image"
),
),
new
RaisedButton
(
onPressed:
()
async
{
File
f
=
await
ImagePicker
.
pickImage
(
source
:
ImageSource
.
gallery
);
ShareExtend
.
share
(
f
.
path
,
"file"
);
},
child:
new
Text
(
"share file"
),
),
new
RaisedButton
(
onPressed:
()
{
if
(
Platform
.
isAndroid
)
{
_shareFileWithPerm
();
}
_shareApplicationDocumentsFile
();
},
child:
new
Text
(
"share
android external storage
file"
),
child:
new
Text
(
"share file"
),
),
],
)),
...
...
@@ -65,44 +54,14 @@ class _MyAppState extends State<MyApp> {
);
}
/// 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"
);
///share the documents file
_shareApplicationDocumentsFile
()
async
{
Directory
dir
=
await
getApplicationDocumentsDirectory
();
File
testFile
=
new
File
(
"
${dir.path}
/flutter/test.txt"
);
if
(!
await
testFile
.
exists
())
{
await
testFile
.
create
(
recursive:
true
);
testFile
.
writeAsStringSync
(
"test for share documents file"
);
}
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
);
ShareExtend
.
share
(
testFile
.
path
,
"file"
);
}
}
example/pubspec.yaml
View file @
a6165e59
...
...
@@ -21,8 +21,6 @@ dependencies:
cupertino_icons
:
^0.1.2
image_picker
:
^0.4.10
path_provider
:
^0.4.1
permission
:
^0.1.0
dev_dependencies
:
flutter_test
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment