feat: Implemented copy image to clipboard on Android

This commit is contained in:
13574
2023-09-09 23:35:17 +08:00
parent e9ddcfe909
commit 9878c1108e
10 changed files with 114 additions and 13 deletions

View File

@@ -2,7 +2,7 @@
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:label="@string/app_name"
android:name="${applicationName}"
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
@@ -12,10 +12,6 @@
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
@@ -25,10 +21,17 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<provider
android:authorities="com.lifegpc.ehf.ClipboardImageProvider"
android:name=".provider.ClipboardImageProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_config"/>
</provider>
</application>
</manifest>

View File

@@ -4,25 +4,27 @@ import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile
import com.lifegpc.ehf.annotation.ChannelMethod
import com.lifegpc.ehf.mmkv.SAFSettings
import com.lifegpc.ehf.data.mmkv.SAFSettings
import com.lifegpc.ehf.util.ClipboardUtils
import com.lifegpc.ehf.util.MethodChannelUtils
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.io.FileOutputStream
import java.lang.Exception
class MainActivity : FlutterActivity() {
private val safAuthorizationCode = 0x10086
private var safAuthorizationResult: MethodChannel.Result? = null
private var afterAuthSuccess:(()->Unit)?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannelUtils.registerMethodChannel("lifegpc.eh_downloader_flutter/saf", flutterEngine, this)
MethodChannelUtils.registerMethodChannel("lifegpc.eh_downloader_flutter/clipboard",flutterEngine,ClipboardUtils)
}
@ChannelMethod(responseManually = true)

View File

@@ -0,0 +1,19 @@
package com.lifegpc.ehf
import android.app.Application
import android.content.Context
class MyApplication:Application() {
companion object{
@JvmStatic
private var mApplicationContext:Context?=null
@JvmStatic
@get:JvmName("_applicationContext")
val applicationContext:Context
get() = mApplicationContext!!
}
override fun onCreate() {
super.onCreate()
mApplicationContext=applicationContext
}
}

View File

@@ -0,0 +1,10 @@
package com.lifegpc.ehf.data.litepal
import org.litepal.crud.LitePalSupport
class ClipboardImageItem(
val uuid:String,
val mimeType:String
):LitePalSupport() {
val id:Long=0
}

View File

@@ -1,4 +1,4 @@
package com.lifegpc.ehf.mmkv
package com.lifegpc.ehf.data.mmkv
import com.dylanc.mmkv.MMKVOwner
import com.dylanc.mmkv.mmkvString

View File

@@ -0,0 +1,6 @@
package com.lifegpc.ehf.provider
import android.net.Uri
import androidx.core.content.FileProvider
class ClipboardImageProvider:FileProvider()

View File

@@ -0,0 +1,46 @@
package com.lifegpc.ehf.util
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import androidx.core.content.FileProvider
import com.lifegpc.ehf.MyApplication
import com.lifegpc.ehf.annotation.ChannelMethod
import java.io.File
import java.util.UUID
object ClipboardUtils {
private const val AUTHORITY="com.lifegpc.ehf.ClipboardImageProvider"
@ChannelMethod
fun copyImageToClipboard(mimeType:String,byteArray: ByteArray){
val file= saveToImageCache(mimeType, byteArray)
val uri=FileProvider.getUriForFile(MyApplication.applicationContext, AUTHORITY,file)
val cbm=MyApplication.applicationContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData=ClipData("image", arrayOf(mimeType),ClipData.Item(uri))
cbm.setPrimaryClip(clipData)
}
private fun saveToImageCache(mimeType:String,byteArray: ByteArray):File{
val dir=File(MyApplication.applicationContext.filesDir,"images")
if (!dir.exists()){
dir.mkdirs()
}
val name=UUID.randomUUID().toString()
val file=File(dir,"$name.${mimeTypeToExtName(mimeType)}")
file.outputStream().use {
it.write(byteArray)
}
return file
}
private fun mimeTypeToExtName(mimeType: String)=when(mimeType){
"image/png"->"png"
"image/jpeg"->"png"
"image/gif"->"gif"
else->throw IllegalArgumentException("$mimeType is not supported")
}
}

View File

@@ -1,4 +1,4 @@
package com.lifegpc.ehf
package com.lifegpc.ehf.util
import android.util.Log
import com.lifegpc.ehf.annotation.ChannelMethod

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="images"
path="images"/>
</paths>

View File

@@ -0,0 +1,9 @@
import 'package:flutter/services.dart';
class Clipboard{
final MethodChannel _clipboardChannel=const MethodChannel("lifegpc.eh_downloader_flutter/clipboard");
Future<void> copyImageToClipboard(String mimeType,Uint8List bytes)async{
return _clipboardChannel.invokeMethod("copyImageToClipboard",[mimeType,bytes]);
}
}