File Copy Flutter Plugin in Android with Kotlin

2019-08-09 16:42发布

问题:

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

回答1:

I have finally found a solution to file copy issue in Kotlin !

It was especially helpful in achieving my very first Flutter plugin.

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