Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
clx-fluwx
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
clx-fluwx
Commits
b8ff3f40
提交
b8ff3f40
authored
5月 13, 2020
作者:
JarvanMo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support share image via FileProvider
上级
103b28f5
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
138 行增加
和
53 行删除
+138
-53
AndroidManifest.xml
android/src/main/AndroidManifest.xml
+12
-2
FluwxFileProvider.kt
...oid/src/main/kotlin/com/jarvan/fluwx/FluwxFileProvider.kt
+11
-0
FluwxShareHandler.kt
...ain/kotlin/com/jarvan/fluwx/handlers/FluwxShareHandler.kt
+40
-16
FileIO.kt
android/src/main/kotlin/com/jarvan/fluwx/io/FileIO.kt
+70
-0
ImagesIO.kt
android/src/main/kotlin/com/jarvan/fluwx/io/ImagesIO.kt
+0
-35
fluwx_file_provider_paths.xml
android/src/main/res/xml/fluwx_file_provider_paths.xml
+5
-0
没有找到文件。
android/src/main/AndroidManifest.xml
浏览文件 @
b8ff3f40
<manifest
xmlns:android=
"http://schemas.android.com/apk/res/android"
package=
"com.jarvan.fluwx"
>
package=
"com.jarvan.fluwx"
>
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<uses-permission
android:name=
"android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name=
"android.permission.ACCESS_WIFI_STATE"
/>
<uses-permission
android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<application>
<activity
...
...
@@ -39,5 +39,15 @@
<data
android:scheme=
"sdksample"
/>
</intent-filter>
</activity-alias>
<provider
android:name=
"com.jarvan.fluwx.FluwxFileProvider"
android:authorities=
"${applicationId}.fluwxprovider"
android:exported=
"false"
android:grantUriPermissions=
"true"
>
<meta-data
android:name=
"android.support.FILE_PROVIDER_PATHS"
android:resource=
"@xml/fluwx_file_provider_paths"
/>
</provider>
</application>
</manifest>
android/src/main/kotlin/com/jarvan/fluwx/FluwxFileProvider.kt
0 → 100644
浏览文件 @
b8ff3f40
package
com.jarvan.fluwx
import
androidx.core.content.FileProvider
/***
* Created by mo on 2020/5/13
* 冷风如刀,以大地为砧板,视众生为鱼肉。
* 万里飞雪,将穹苍作烘炉,熔万物为白银。
**/
class
FluwxFileProvider
:
FileProvider
()
\ No newline at end of file
android/src/main/kotlin/com/jarvan/fluwx/handlers/FluwxShareHandler.kt
浏览文件 @
b8ff3f40
...
...
@@ -2,15 +2,15 @@ package com.jarvan.fluwx.handlers
import
android.Manifest
import
android.content.Context
import
android.content.Intent
import
android.content.pm.PackageManager
import
android.content.res.AssetFileDescriptor
import
android.net.Uri
import
android.text.TextUtils
import
android.util.Log
import
androidx.core.content.ContextCompat
import
com.jarvan.fluwx.io.ImagesIO
import
com.jarvan.fluwx.io.ImagesIOIml
import
com.jarvan.fluwx.io.WeChatImage
import
com.jarvan.fluwx.io.toExternalCacheFile
import
androidx.core.content.FileProvider
import
com.jarvan.fluwx.io.*
import
com.tencent.mm.opensdk.modelbase.BaseReq
import
com.tencent.mm.opensdk.modelmsg.*
import
io.flutter.embedding.engine.plugins.FlutterPlugin
...
...
@@ -18,16 +18,17 @@ import io.flutter.plugin.common.MethodCall
import
io.flutter.plugin.common.MethodChannel
import
io.flutter.plugin.common.PluginRegistry
import
kotlinx.coroutines.*
import
java.io.File
import
java.util.*
import
kotlin.coroutines.CoroutineContext
/***
* Created by mo on 2020/3/6
* 冷风如刀,以大地为砧板,视众生为鱼肉。
* 万里飞雪,将穹苍作烘炉,熔万物为白银。
**/
internal
class
FluwxShareHandlerEmbedding
(
private
val
flutterAssets
:
FlutterPlugin
.
FlutterAssets
,
override
val
context
:
Context
)
:
FluwxShareHandler
{
override
val
assetFileDescriptor
:
(
String
)
->
AssetFileDescriptor
=
{
val
uri
=
Uri
.
parse
(
it
)
val
packageName
=
uri
.
getQueryParameter
(
"package"
)
...
...
@@ -135,10 +136,14 @@ internal interface FluwxShareHandler : CoroutineScope {
}
sourceByteArray
.
size
>
512
*
1024
->
{
WXImageObject
().
apply
{
if
(
ContextCompat
.
checkSelfPermission
(
context
,
Manifest
.
permission
.
WRITE_EXTERNAL_STORAGE
)
==
PackageManager
.
PERMISSION_GRANTED
)
{
setImagePath
(
sourceByteArray
.
toExternalCacheFile
(
context
,
sourceImage
.
suffix
)
?.
absolutePath
)
if
(
supportFileProvider
&&
targetHigherThanN
)
{
setImagePath
(
getFileContentUri
(
sourceByteArray
.
toCacheFile
(
context
,
sourceImage
.
suffix
))
)
}
else
{
permissionHandler
?.
requestStoragePermission
()
if
(
ContextCompat
.
checkSelfPermission
(
context
,
Manifest
.
permission
.
WRITE_EXTERNAL_STORAGE
)
==
PackageManager
.
PERMISSION_GRANTED
)
{
setImagePath
(
sourceByteArray
.
toExternalCacheFile
(
context
,
sourceImage
.
suffix
)
?.
absolutePath
)
}
else
{
permissionHandler
?.
requestStoragePermission
()
}
}
}
}
...
...
@@ -150,7 +155,7 @@ internal interface FluwxShareHandler : CoroutineScope {
msg
.
mediaObject
=
imageObject
msg
.
thumbData
=
thumbData
msg
.
description
=
call
.
argument
<
String
>
(
keyDescription
)
msg
.
description
=
call
.
argument
(
keyDescription
)
val
req
=
SendMessageToWX
.
Req
()
setCommonArguments
(
call
,
req
,
msg
)
...
...
@@ -261,11 +266,11 @@ internal interface FluwxShareHandler : CoroutineScope {
// SESSION, TIMELINE, FAVORITE
private
fun
setCommonArguments
(
call
:
MethodCall
,
req
:
SendMessageToWX
.
Req
,
msg
:
WXMediaMessage
)
{
msg
.
messageAction
=
call
.
argument
<
String
?>
(
"messageAction"
)
msg
.
messageExt
=
call
.
argument
<
String
?>
(
"messageExt"
)
msg
.
mediaTagName
=
call
.
argument
<
String
?>
(
"mediaTagName"
)
msg
.
title
=
call
.
argument
<
String
?>
(
keyTitle
)
msg
.
description
=
call
.
argument
<
String
?>
(
keyDescription
)
msg
.
messageAction
=
call
.
argument
(
"messageAction"
)
msg
.
messageExt
=
call
.
argument
(
"messageExt"
)
msg
.
mediaTagName
=
call
.
argument
(
"mediaTagName"
)
msg
.
title
=
call
.
argument
(
keyTitle
)
msg
.
description
=
call
.
argument
(
keyDescription
)
req
.
transaction
=
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
)
val
sceneIndex
=
call
.
argument
<
Int
?>(
"scene"
)
req
.
scene
=
when
(
sceneIndex
)
{
...
...
@@ -276,6 +281,25 @@ internal interface FluwxShareHandler : CoroutineScope {
}
}
private
fun
getFileContentUri
(
file
:
File
?):
String
?
{
if
(
file
==
null
||
!
file
.
exists
())
return
null
val
contentUri
=
FileProvider
.
getUriForFile
(
context
,
"${context.packageName}.fluwxprovider"
,
// 要与`AndroidManifest.xml`里配置的`authorities`一致,假设你的应用包名为com.example.app
file
)
// 授权给微信访问路径
context
.
grantUriPermission
(
"com.tencent.mm"
,
// 这里填微信包名
contentUri
,
Intent
.
FLAG_GRANT_READ_URI_PERMISSION
)
return
contentUri
.
toString
()
// contentUri.toString() 即是以"content://"开头的用于共享的路径
}
private
val
supportFileProvider
:
Boolean
get
()
=
WXAPiHandler
.
wxApi
?.
wxAppSupportAPI
?:
0
>=
0
x27000D00
private
val
targetHigherThanN
:
Boolean
get
()
=
android
.
os
.
Build
.
VERSION
.
SDK_INT
>=
android
.
os
.
Build
.
VERSION_CODES
.
N
val
context
:
Context
val
assetFileDescriptor
:
(
String
)
->
AssetFileDescriptor
...
...
@@ -288,4 +312,5 @@ internal interface FluwxShareHandler : CoroutineScope {
var
permissionHandler
:
PermissionHandler
?
fun
onDestroy
()
=
job
.
cancel
()
}
\ No newline at end of file
}
android/src/main/kotlin/com/jarvan/fluwx/io/FileIO.kt
0 → 100644
浏览文件 @
b8ff3f40
package
com.jarvan.fluwx.io
import
android.content.Context
import
android.util.Log
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.withContext
import
okio.*
import
java.io.*
import
java.util.*
/***
* Created by mo on 2020/5/13
* 冷风如刀,以大地为砧板,视众生为鱼肉。
* 万里飞雪,将穹苍作烘炉,熔万物为白银。
**/
private
const
val
cachePathName
=
"fluwxSharedData"
internal
suspend
fun
ByteArray
.
toExternalCacheFile
(
context
:
Context
,
suffix
:
String
):
File
?
{
var
file
:
File
?
=
null
val
externalFile
=
context
.
externalCacheDir
?:
return
file
val
dir
=
File
(
externalFile
.
absolutePath
+
File
.
separator
+
cachePathName
).
apply
{
if
(!
exists
())
{
mkdirs
()
}
}
file
=
File
(
dir
.
absolutePath
+
File
.
separator
+
UUID
.
randomUUID
().
toString
()
+
suffix
)
return
saveToLocal
(
this
,
file
)
}
internal
suspend
fun
ByteArray
.
toCacheFile
(
context
:
Context
,
suffix
:
String
):
File
?
{
var
file
:
File
?
=
null
val
externalFile
=
context
.
cacheDir
?:
return
file
val
dir
=
File
(
externalFile
.
absolutePath
+
File
.
separator
+
cachePathName
).
apply
{
if
(!
exists
())
{
mkdirs
()
}
}
file
=
File
(
dir
.
absolutePath
+
File
.
separator
+
UUID
.
randomUUID
().
toString
()
+
suffix
)
return
saveToLocal
(
this
,
file
)
}
private
suspend
fun
saveToLocal
(
byteArray
:
ByteArray
,
file
:
File
):
File
?
{
return
withContext
(
Dispatchers
.
IO
)
{
var
sink
:
BufferedSink
?
=
null
var
source
:
Source
?
=
null
var
outputStream
:
OutputStream
?
=
null
try
{
outputStream
=
FileOutputStream
(
file
)
sink
=
outputStream
.
sink
().
buffer
()
source
=
ByteArrayInputStream
(
byteArray
).
source
()
sink
.
writeAll
(
source
)
sink
.
flush
()
}
catch
(
e
:
IOException
)
{
Log
.
w
(
"Fluwx"
,
"failed to create cache files"
)
}
finally
{
sink
?.
close
()
source
?.
close
()
outputStream
?.
close
()
}
file
}
}
android/src/main/kotlin/com/jarvan/fluwx/io/ImagesIO.kt
浏览文件 @
b8ff3f40
...
...
@@ -119,37 +119,3 @@ interface ImagesIO {
suspend
fun
readByteArray
():
ByteArray
suspend
fun
compressedByteArray
(
context
:
Context
,
maxSize
:
Int
):
ByteArray
}
internal
suspend
fun
ByteArray
.
toExternalCacheFile
(
context
:
Context
,
suffix
:
String
):
File
?
{
val
byteArray
=
this
return
withContext
(
Dispatchers
.
IO
)
{
var
file
:
File
?
=
null
var
sink
:
BufferedSink
?
=
null
var
source
:
Source
?
=
null
var
outputStream
:
OutputStream
?
=
null
try
{
val
externalFile
=
context
.
externalCacheDir
?:
return
@withContext
file
file
=
File
(
externalFile
.
absolutePath
+
File
.
separator
+
UUID
.
randomUUID
().
toString
()
+
suffix
)
outputStream
=
FileOutputStream
(
file
)
sink
=
outputStream
.
sink
().
buffer
()
source
=
ByteArrayInputStream
(
byteArray
).
source
()
sink
.
writeAll
(
source
)
sink
.
flush
()
}
catch
(
e
:
IOException
)
{
Log
.
w
(
"Fluwx"
,
"failed to create external files"
)
}
finally
{
sink
?.
close
()
source
?.
close
()
outputStream
?.
close
()
}
file
}
}
\ No newline at end of file
android/src/main/res/xml/fluwx_file_provider_paths.xml
0 → 100644
浏览文件 @
b8ff3f40
<?xml version="1.0" encoding="utf-8"?>
<paths
xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<cache-path
name=
"fluwxSharedData"
path=
"fluwxSharedData/"
/>
</paths>
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论