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
4eb8ee25
Commit
4eb8ee25
authored
Dec 18, 2018
by
zhouteng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
将android 7.0中FileProvider获取url的方法集成到插件库中
parent
409afdac
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
146 additions
and
79 deletions
+146
-79
android/src/main/AndroidManifest.xml
android/src/main/AndroidManifest.xml
+15
-1
android/src/main/java/com/zt/shareextend/ShareExtendPlugin.java
...d/src/main/java/com/zt/shareextend/ShareExtendPlugin.java
+64
-59
android/src/main/res/xml/file_provider_path.xml
android/src/main/res/xml/file_provider_path.xml
+8
-0
example/android/app/src/main/AndroidManifest.xml
example/android/app/src/main/AndroidManifest.xml
+1
-10
example/lib/main.dart
example/lib/main.dart
+54
-6
example/pubspec.yaml
example/pubspec.yaml
+2
-0
lib/share_extend.dart
lib/share_extend.dart
+2
-3
No files found.
android/src/main/AndroidManifest.xml
View file @
4eb8ee25
<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>
android/src/main/java/com/zt/shareextend/ShareExtendPlugin.java
View file @
4eb8ee25
...
...
@@ -15,74 +15,79 @@ import android.support.v4.content.FileProvider;
import
java.io.File
;
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
{
private
static
final
String
CHANNEL
=
"share_extend"
;
public
static
void
registerWith
(
Registrar
registrar
)
{
MethodChannel
channel
=
new
MethodChannel
(
registrar
.
messenger
(),
CHANNEL
);
ShareExtendPlugin
instance
=
new
ShareExtendPlugin
(
registrar
);
channel
.
setMethodCallHandler
(
instance
);
}
private
final
Registrar
mRegistrar
;
private
ShareExtendPlugin
(
Registrar
registrar
)
{
this
.
mRegistrar
=
registrar
;
}
@Override
public
void
onMethodCall
(
MethodCall
call
,
MethodChannel
.
Result
result
)
{
if
(
call
.
method
.
equals
(
"share"
))
{
if
(!(
call
.
arguments
instanceof
Map
))
{
throw
new
IllegalArgumentException
(
"Map argument expected"
);
}
// 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"
));
result
.
success
(
null
);
}
else
{
result
.
notImplemented
();
private
static
final
String
authorities
=
"com.zt.shareextend.fileprovider"
;
private
static
final
String
CHANNEL
=
"share_extend"
;
public
static
void
registerWith
(
Registrar
registrar
)
{
MethodChannel
channel
=
new
MethodChannel
(
registrar
.
messenger
(),
CHANNEL
);
ShareExtendPlugin
instance
=
new
ShareExtendPlugin
(
registrar
);
channel
.
setMethodCallHandler
(
instance
);
}
}
private
void
share
(
String
text
,
String
type
,
String
authorities
)
{
if
(
text
==
null
||
text
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"Non-empty text expected"
);
private
final
Registrar
mRegistrar
;
private
ShareExtendPlugin
(
Registrar
registrar
)
{
this
.
mRegistrar
=
registrar
;
}
Intent
shareIntent
=
new
Intent
();
shareIntent
.
setAction
(
Intent
.
ACTION_SEND
);
if
(
"text"
.
equals
(
type
))
{
shareIntent
.
putExtra
(
Intent
.
EXTRA_TEXT
,
text
);
shareIntent
.
setType
(
"text/plain"
);
}
else
{
File
f
=
new
File
(
text
);
Uri
uri
=
getUriForFile
(
mRegistrar
.
context
(),
authorities
,
f
);
if
(
"file"
.
equals
(
type
))
{
shareIntent
.
setType
(
"application/*"
);
}
if
(
"image"
.
equals
(
type
))
{
shareIntent
.
setType
(
"image/*"
);
}
shareIntent
.
putExtra
(
Intent
.
EXTRA_STREAM
,
uri
);
@Override
public
void
onMethodCall
(
MethodCall
call
,
MethodChannel
.
Result
result
)
{
if
(
call
.
method
.
equals
(
"share"
))
{
if
(!(
call
.
arguments
instanceof
Map
))
{
throw
new
IllegalArgumentException
(
"Map argument expected"
);
}
// Android does not support showing the share sheet at a particular point on screen.
share
((
String
)
call
.
argument
(
"text"
),
(
String
)
call
.
argument
(
"type"
));
result
.
success
(
null
);
}
else
{
result
.
notImplemented
();
}
}
Intent
chooserIntent
=
Intent
.
createChooser
(
shareIntent
,
null
/* dialog title optional */
);
if
(
mRegistrar
.
activity
()
!=
null
)
{
mRegistrar
.
activity
().
startActivity
(
chooserIntent
);
}
else
{
chooserIntent
.
addFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
);
mRegistrar
.
context
().
startActivity
(
chooserIntent
);
private
void
share
(
String
text
,
String
type
)
{
if
(
text
==
null
||
text
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"Non-empty text expected"
);
}
Intent
shareIntent
=
new
Intent
();
shareIntent
.
setAction
(
Intent
.
ACTION_SEND
);
if
(
"text"
.
equals
(
type
))
{
shareIntent
.
putExtra
(
Intent
.
EXTRA_TEXT
,
text
);
shareIntent
.
setType
(
"text/plain"
);
}
else
{
File
f
=
new
File
(
text
);
Uri
uri
=
getUriForFile
(
mRegistrar
.
context
(),
f
);
if
(
"file"
.
equals
(
type
))
{
shareIntent
.
setType
(
"application/*"
);
}
if
(
"image"
.
equals
(
type
))
{
shareIntent
.
setType
(
"image/*"
);
}
shareIntent
.
putExtra
(
Intent
.
EXTRA_STREAM
,
uri
);
}
Intent
chooserIntent
=
Intent
.
createChooser
(
shareIntent
,
null
/* dialog title optional */
);
if
(
mRegistrar
.
activity
()
!=
null
)
{
mRegistrar
.
activity
().
startActivity
(
chooserIntent
);
}
else
{
chooserIntent
.
addFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
);
mRegistrar
.
context
().
startActivity
(
chooserIntent
);
}
}
}
public
static
Uri
getUriForFile
(
Context
context
,
String
authories
,
File
file
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
24
)
{
return
FileProvider
.
getUriForFile
(
context
,
authories
,
file
);
}
else
{
return
Uri
.
fromFile
(
file
);
public
static
Uri
getUriForFile
(
Context
context
,
File
file
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
24
)
{
return
FileProvider
.
getUriForFile
(
context
,
authorities
,
file
);
}
else
{
return
Uri
.
fromFile
(
file
);
}
}
}
}
example/android/app
/src/main/res/xml/file_provider_path.xml
→
android
/src/main/res/xml/file_provider_path.xml
View file @
4eb8ee25
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<
external
-path
name=
"
external_cache
"
path=
""
/>
<
root
-path
name=
"
root_path
"
path=
"
/
"
/>
</paths>
</resources>
\ No newline at end of file
example/android/app/src/main/AndroidManifest.xml
View file @
4eb8ee25
...
...
@@ -6,6 +6,7 @@
to allow setting breakpoints, to provide hot reload, etc.
-->
<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
calls FlutterMain.startInitialization(this); in its onCreate method.
...
...
@@ -36,15 +37,5 @@
</intent-filter>
</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>
</manifest>
example/lib/main.dart
View file @
4eb8ee25
...
...
@@ -3,6 +3,8 @@ 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
());
...
...
@@ -12,9 +14,6 @@ class MyApp extends StatefulWidget {
}
class
_MyAppState
extends
State
<
MyApp
>
{
///define in android project AndroidManifest.xml for FileProvider
static
const
String
authorities
=
"com.zt.shareextend.fileprovider"
;
@override
void
initState
()
{
super
.
initState
();
...
...
@@ -32,7 +31,7 @@ class _MyAppState extends State<MyApp> {
children:
<
Widget
>[
new
RaisedButton
(
onPressed:
()
{
ShareExtend
.
share
(
"share text"
,
"text"
,
authorities
);
ShareExtend
.
share
(
"share text"
,
"text"
);
},
child:
new
Text
(
"share text"
),
),
...
...
@@ -40,7 +39,7 @@ class _MyAppState extends State<MyApp> {
onPressed:
()
async
{
File
f
=
await
ImagePicker
.
pickImage
(
source
:
ImageSource
.
gallery
);
ShareExtend
.
share
(
f
.
path
,
"image"
,
authorities
);
ShareExtend
.
share
(
f
.
path
,
"image"
);
},
child:
new
Text
(
"share image"
),
),
...
...
@@ -48,13 +47,62 @@ class _MyAppState extends State<MyApp> {
onPressed:
()
async
{
File
f
=
await
ImagePicker
.
pickImage
(
source
:
ImageSource
.
gallery
);
ShareExtend
.
share
(
f
.
path
,
"file"
,
authorities
);
ShareExtend
.
share
(
f
.
path
,
"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
);
}
}
example/pubspec.yaml
View file @
4eb8ee25
...
...
@@ -20,6 +20,8 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons
:
^0.1.2
image_picker
:
^0.4.10
path_provider
:
^0.4.1
permission
:
^0.1.0
dev_dependencies
:
...
...
lib/share_extend.dart
View file @
4eb8ee25
...
...
@@ -18,14 +18,13 @@ class ShareExtend {
/// on iOS.
/// type "text", "image" ,"file"
///
static
Future
<
void
>
share
(
String
text
,
String
type
,
String
authorities
,
static
Future
<
void
>
share
(
String
text
,
String
type
,
{
Rect
sharePositionOrigin
})
{
assert
(
text
!=
null
);
assert
(
text
.
isNotEmpty
);
final
Map
<
String
,
dynamic
>
params
=
<
String
,
dynamic
>{
'text'
:
text
,
'type'
:
type
,
'authorities'
:
authorities
'type'
:
type
};
if
(
sharePositionOrigin
!=
null
)
{
...
...
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