2

Im trying to load an asset from flutter project "asset" folder into native code. For example I want this file /assets/labels.txt (Flutter folder) to load it in a Kotlin script in the path android/app/main/kotlin/MainActivity.kt.

I successfully connect Flutter into Kotlin but I have errors when I try to load it.

class MainActivity : FlutterActivity() {
    private val CHANNEL = "your_channel_name"
    private val LABELS_PATH = "assets/labels.txt"  // 

    private fun loadLabels() {
        try {
            Log.d("LoadLabels", "Loading labels...")

            val reader = BufferedReader(InputStreamReader(assets.open(LABELS_PATH)))
            labels = reader.readLines()
            reader.close()
            labels.forEach { line ->
                Log.d("TextFileContent", line)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

// ...
}

These are my errors :

D/LoadLabels(11123): Loading labels...
W/System.err(11123): java.io.FileNotFoundException: assets/labels.txt

So my question is this :

I must load the assets always from Flutter project folder or from the same folder that the native code script is? Also what is my error here?

Thanks.

3
  • Does this answer your question? stackoverflow.com/questions/70809816/… Commented Feb 8, 2024 at 2:10
  • @RichardHeap that's for plugin. i want to know how to do that in activity. Commented Feb 8, 2024 at 14:41
  • Im a little bit confused also about activity and plugins , i just want to run a specific function for tensorflow(some audio classifications). Do i have to do this as a plugin? Also why i have to keep the binding?First time saw that im confused,every time i hit a function on native code i must to a binding too? Thanks again. Commented Feb 8, 2024 at 15:26

1 Answer 1

0

Analyze your flutter apk in as and you will find that it has a directory structure like this

enter image description here

Now suppose I need to load pic_app_logo.png, then the code should be like this

context.assets.open("flutter_assets/packages/pkg_res/assets/images/common/login/pic_app_logo.png")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.