带有Kotlin的Android中的文件复制Flutter插件 [英] File Copy Flutter Plugin in Android with Kotlin
问题描述
试图创建一个Flutter插件,该插件将资产文件复制到本机的Application Documents文件夹中.
Trying to create a Flutter plugin that copies an asset file to the native Application Documents Folder.
对于iOS,我是通过以下代码(见下文)实现的.
For iOS, I achieved this by the following code (see below).
但是,由于我对Android体系结构了解不多,所以我想知道我的Android MethodChannel代码应该是什么样子.
However, since I do not have much knowledge of the Android architecture, I would like to know how my Android MethodChannel code should look like.
我的Flutter插件的Android部分必须位于KOTLIN中!
My Android part of this Flutter plugin needs to be in KOTLIN !
我需要将文件从Android资产文件夹复制到Android的Documents文件夹-所有这些操作都在Flutter插件和Kotlin中完成!
I need a file copy from the Android assets folder to the Documents Folder of Android - all this done inside the Flutter plugin and in Kotlin!
同样,我已经准备好在Swift中安装iOS.缺少的是Kotlin计数器中的Android.您对此有任何帮助吗?
Again, I have iOS in Swift ready made. What is missing is the Android in Kotlin counter part. Do you have any help on this ?
.
这是Swift中iOS FlutterMethodChannel的工作代码:
Here is the working code for the iOS FlutterMethodChannel in Swift:
(即它将文件从主捆绑包复制到iPhone的文档目录...)
(i.e. it copies a file from the main-bundle to the Documents-Directory of the iPhone...)
import UIKit
private func copyFile(fileName: String) -> String {
let fileManager = FileManager.default
let documentsUrl = fileManager.urls(for: .documentDirectory,
in: .userDomainMask)
guard documentsUrl.count != 0 else {
return "Could not find documents URL"
}
let finalURL = documentsUrl.first!.appendingPathComponent(fileName)
if !( (try? finalURL.checkResourceIsReachable()) ?? false) {
let documentsURL = Bundle.main.resourceURL?.appendingPathComponent(fileName)
do {
try fileManager.copyItem(atPath: (documentsURL?.path)!, toPath: finalURL.path)
return "\(finalURL.path)"
} catch let error as NSError {
return "Couldn't copy file to final location! Error:\(error.description)"
}
} else {
return "\(finalURL.path)"
}
}
在Kotlin,我尝试了此方法-但这根本不起作用....:(
In Kotlin, I tried this - but it does not work at all....:(
import java.io.File
private fun copyFileTrial1(fileName: String): String {
File src = new File("../../assets/${fileName}");
File dst = new File("../../DocumentsFolder/${fileName}", src.getName());
FileInputStream inStream = new FileInputStream(src);
FileOutputStream outStream = new FileOutputStream(dst);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
return "hello1"
}
或者我尝试了这个-但是再次-完全没有成功:(
Or I tried this - but again - completely without success :(
private fun copyFileTrial2(fileName: String): String {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(fileName);
String outDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ;
File outFile = new File(outDir, filenfileNameame);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + fileName, e);
}
return "hello2"
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
推荐答案
我终于在Kotlin中找到了解决文件复制问题的解决方案!
I have finally found a solution to file copy issue in Kotlin !
这是Kotlin中文件复制的解决方案
Here is the solution of the file-copy in Kotlin
import java.io.File
import java.io.InputStream
import io.flutter.util.PathUtils
private fun copyFile(fileName: String): String {
val assetStream: InputStream = mRegistrar.context().assets.open(fileName)
val appliationDocumentsFolderPath: String = PathUtils.getDataDirectory(mRegistrar.context())
val outputFilePath: String = appliationDocumentsFolderPath + "/" + fileName
if (!File(outputFilePath).exists()) {
File(outputFilePath).copyInputStreamToFile(assetStream)
}
return outputFilePath
}
private fun File.copyInputStreamToFile(inputStream: InputStream) {
inputStream.use { input ->
this.outputStream().use { fileOut ->
input.copyTo(fileOut)
}
}
}
这篇关于带有Kotlin的Android中的文件复制Flutter插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!