How do I get the Assets path in an Android Studio project?

社会主义新天地 提交于 2019-12-25 04:23:20

问题


I feel like I'm missing something obvious, but I'm having trouble passing a CSV file to CSVReader, which takes a Reader in its constructor. I can use AssetManager.getAssets(), but that returns an InputStream and I can't get the file path from there, which the Reader constructor needs. My asset path is

src/main/assets/dbsource/My.csv

I need to get this into

CSVReader csvReader = new CSVReader(new FileReader(csvPath));

I just can't get the path! Thanks.


回答1:


It's difficult yo tell you what you can do without knowing which CSVReader you're using, but, assuming that you're using this one you can easily use InputStreamReader instead of the FileReader here, because it accepts any Reader subclasses.

Perhaps the even better way would be to wrap up the InputStreamReader into the BufferedReader like so:

final CSVReader csvReader = new BufferedReader(new InputStreamReader(
    context.getAssets().open("dbsource/My.csv")
));


来源:https://stackoverflow.com/questions/28979778/how-do-i-get-the-assets-path-in-an-android-studio-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!