How to get FileInputStream to File in assets folder

前端 未结 1 1422
太阳男子
太阳男子 2020-12-09 11:16

I know how to use the AssetManager to read a File from the res/raw directory with an InputStream, but for my special use

相关标签:
1条回答
  • 2020-12-09 12:14

    You can get a FileInputStream to a resource in your assets like this:

    AssetFileDescriptor fileDescriptor = assetManager.openFd(fileName);
    FileInputStream stream = fileDescriptor.createInputStream();
    

    The fileName you supply to openFd() should be the relative path to the asset, the same fileName you would supply to open().

    Alternatively you can also create the FileInputStream like this:

    AssetFileDescriptor assetFileDescriptor = assetManager.openFd(fileName);  
    FileDescriptor fileDescriptor = assetFileDescriptor.getFileDescriptor();  
    FileInputStream stream = new FileInputStream(fileDescriptor);
    
    0 讨论(0)
提交回复
热议问题