read file from assets

前端 未结 18 2220
终归单人心
终归单人心 2020-11-21 22:47
public class Utils {
    public static List getMessages() {
        //File file = new File(\"file:///android_asset/helloworld.txt\");
        AssetMan         


        
18条回答
  •  你的背包
    2020-11-21 23:27

    Here is a way to get an InputStream for a file in the assets folder without a Context, Activity, Fragment or Application. How you get the data from that InputStream is up to you. There are plenty of suggestions for that in other answers here.

    Kotlin

    val inputStream = ClassLoader::class.java.classLoader?.getResourceAsStream("assets/your_file.ext")
    

    Java

    InputStream inputStream = ClassLoader.class.getClassLoader().getResourceAsStream("assets/your_file.ext");
    

    All bets are off if a custom ClassLoader is in play.

提交回复
热议问题