flutter java kotlin,带有Kotlin的Android中的文件复制Flutter插件

Trying to create a Flutter plugin that copies an asset file to the native Application Documents Folder.

For iOS, I achieved this by the following code (see below).

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.

My Android part of this Flutter plugin needs to be in 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!

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 ?

.

Here is the working code for the iOS FlutterMethodChannel in Swift:

(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)"

}

}

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);

}

}

解决方案

I have finally found a solution to file copy issue in 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)

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值