How to get URI from an asset File?

后端 未结 11 1664
慢半拍i
慢半拍i 2020-11-22 14:27

I have been trying to get the URI path for an asset file.

uri = Uri.fromFile(new File(\"//assets/mydemo.txt\"));

When I check if the file

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 14:52

    The correct url is:

    file:///android_asset/RELATIVEPATH
    

    where RELATIVEPATH is the path to your resource relative to the assets folder.

    Note the 3 /'s in the scheme. Web view would not load any of my assets without the 3. I tried 2 as (previously) commented by CommonsWare and it wouldn't work. Then I looked at CommonsWare's source on github and noticed the extra forward slash.

    This testing though was only done on the 1.6 Android emulator but I doubt its different on a real device or higher version.

    EDIT: CommonsWare updated his answer to reflect this tiny change. So I've edited this so it still makes sense with his current answer.

提交回复
热议问题