feat: additional clipboard method
This commit is contained in:
parent
c77338948e
commit
6f4b76353a
6 changed files with 38 additions and 10 deletions
|
@ -264,7 +264,7 @@ class FileColumn(val context: Context) {
|
|||
f.mkdir()
|
||||
}
|
||||
ClipHelper.getInstance(context).copy(source, context)
|
||||
val sourceUri = ClipHelper.getInstance(context).paste() ?: return false
|
||||
val sourceUri = ClipHelper.getInstance(context).paste(context) ?: return false
|
||||
val inputStream = try {
|
||||
context.contentResolver.openInputStream(sourceUri)
|
||||
} catch (e: FileNotFoundException) {
|
||||
|
@ -278,7 +278,7 @@ class FileColumn(val context: Context) {
|
|||
inputStream.close()
|
||||
}
|
||||
ClipHelper.getInstance(context).copy(target, context)
|
||||
val targetUri = ClipHelper.getInstance(context).paste() ?: return false
|
||||
val targetUri = ClipHelper.getInstance(context).paste(context) ?: return false
|
||||
val inputStream2 = context.contentResolver.openInputStream(targetUri)
|
||||
if (inputStream2 != null) {
|
||||
val actualFile = File(f, target.name)
|
||||
|
@ -292,7 +292,7 @@ class FileColumn(val context: Context) {
|
|||
).show()
|
||||
} else if (source.isDirectory) {
|
||||
ClipHelper.getInstance(context).copy(target, context)
|
||||
val sourceUri = ClipHelper.getInstance(context).paste() ?: return false.also {
|
||||
val sourceUri = ClipHelper.getInstance(context).paste(context) ?: return false.also {
|
||||
dropTarget = null
|
||||
}
|
||||
val inputStream = try {
|
||||
|
@ -316,7 +316,7 @@ class FileColumn(val context: Context) {
|
|||
} else if (target.isDirectory) {
|
||||
if (source.isFile) {
|
||||
ClipHelper.getInstance(context).copy(source, context)
|
||||
val sourceUri = ClipHelper.getInstance(context).paste() ?: return false.also {
|
||||
val sourceUri = ClipHelper.getInstance(context).paste(context) ?: return false.also {
|
||||
dropTarget = null
|
||||
}
|
||||
val inputStream = try {
|
||||
|
@ -621,7 +621,7 @@ class FileColumn(val context: Context) {
|
|||
onClick = {
|
||||
AlertHelper.showOnlyPasteInfoNewAlert(context,
|
||||
onPaste = {
|
||||
val uri = ClipHelper.getInstance(context).paste()
|
||||
val uri = ClipHelper.getInstance(context).paste(context)
|
||||
if (uri != null) {
|
||||
val name = uri.path?.split('/')?.last() ?: "somePastedItem"
|
||||
val ext = name.split('.').last()
|
||||
|
|
|
@ -162,7 +162,7 @@ class document_page : AppCompatActivity() {
|
|||
}
|
||||
},
|
||||
onPaste = {
|
||||
val uri = ClipHelper.getInstance(this).paste()
|
||||
val uri = ClipHelper.getInstance(this).paste(this)
|
||||
if (uri != null) {
|
||||
val dir = File(pasteDir)
|
||||
if (!dir.exists()) {
|
||||
|
|
|
@ -143,7 +143,7 @@ class music_page : AppCompatActivity() {
|
|||
}
|
||||
},
|
||||
onPaste = {
|
||||
val uri = ClipHelper.getInstance(this).paste()
|
||||
val uri = ClipHelper.getInstance(this).paste(this)
|
||||
if (uri != null) {
|
||||
val dir = File(pasteDir)
|
||||
if (!dir.exists()) {
|
||||
|
|
|
@ -144,7 +144,7 @@ class picture_page : AppCompatActivity() {
|
|||
}
|
||||
},
|
||||
onPaste = {
|
||||
val uri = ClipHelper.getInstance(this).paste()
|
||||
val uri = ClipHelper.getInstance(this).paste(this)
|
||||
if (uri != null) {
|
||||
val dir = File(pasteDir)
|
||||
if (!dir.exists()) {
|
||||
|
|
|
@ -7,9 +7,16 @@ import android.content.Context
|
|||
import android.net.Uri
|
||||
import androidx.core.content.FileProvider
|
||||
import com.dazuoye.filemanager.BuildConfig
|
||||
import com.dazuoye.filemanager.fileSystem.DeleteHelper.Companion.delete
|
||||
import kotlinx.coroutines.InternalCoroutinesApi
|
||||
import kotlinx.coroutines.internal.synchronized
|
||||
import org.apache.commons.io.IOUtils
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.FileWriter
|
||||
import java.io.InputStream
|
||||
import java.nio.charset.CharsetDecoder
|
||||
|
||||
class ClipHelper private constructor(context: Context) {
|
||||
companion object {
|
||||
|
@ -36,12 +43,33 @@ class ClipHelper private constructor(context: Context) {
|
|||
)
|
||||
val clip = ClipData.newUri(contentResolver, label, uri)
|
||||
clipboard.setPrimaryClip(clip)
|
||||
|
||||
// 备用的复制方法
|
||||
val clipFile = File(context.cacheDir,"clipboard")
|
||||
if (clipFile.exists()){
|
||||
delete(clipFile.path)
|
||||
}
|
||||
|
||||
clipFile.createNewFile()
|
||||
val fw = FileWriter(clipFile)
|
||||
fw.write(uri.toString())
|
||||
fw.close()
|
||||
}
|
||||
|
||||
fun paste(): Uri? {
|
||||
fun paste(context: Context): Uri? {
|
||||
val clip = clipboard.primaryClip
|
||||
clip?.run {
|
||||
val item: ClipData.Item = getItemAt(0)
|
||||
if (item.uri == null){
|
||||
// 备用粘贴方式
|
||||
val clipFile = File(context.cacheDir,"clipboard")
|
||||
if (clipFile.isFile()){
|
||||
val input = FileInputStream(clipFile)
|
||||
val content = IOUtils.toString(input,"UTF-8")
|
||||
return Uri.parse(content)
|
||||
}
|
||||
}
|
||||
|
||||
return item.uri
|
||||
}
|
||||
return null
|
||||
|
|
|
@ -143,7 +143,7 @@ class video_page : AppCompatActivity() {
|
|||
}
|
||||
},
|
||||
onPaste = {
|
||||
val uri = ClipHelper.getInstance(this).paste()
|
||||
val uri = ClipHelper.getInstance(this).paste(this)
|
||||
if (uri != null) {
|
||||
val dir = File(pasteDir)
|
||||
if (!dir.exists()) {
|
||||
|
|
Loading…
Reference in a new issue