开发者

Android AssetManager/InputStream Exception

开发者 https://www.devze.com 2023-01-06 13:23 出处:网络
Doing Android 2.1 development. Can anybody explain to me why the following code generates a IOException and doesn\'t load the file? This exact code used to work, and as far as I can tell, it should st

Doing Android 2.1 development. Can anybody explain to me why the following code generates a IOException and doesn't load the file? This exact code used to work, and as far as I can tell, it should still work. For reference, the开发者_开发问答 Log.d() command correctly lists all files that I expect, and the files are correctly zipped into my .APK file.

        AssetManager assetManager = mContext.getAssets();
        String[] files = null;
        try
            {
            files = assetManager.list("meshes");
            for (int i = 0; i < files.length; i++)
                Log.d(TAG, files[i]);
            InputStream is = assetManager.open(files[0]);
            }
        catch (IOException e) 
            {
            Log.e(TAG, "Could not load '" + e.getMessage()+ "'!");
            }

Any ideas on why this breaks now? The files I'm trying to read are tiny (couple of bytes) binaries.


Finally figured it out. Blatant user error, but couldn't see the forest for the trees. The list command doesn't include the directory name, the open command expects the full pathname. Duh :) The open command needs to build a string that includes both the directory name and the filename.


maybe you can use like this

    AssetManager assetManager = this.getAssets();
    try{
            InputStream is = assetManager.open("tes.png");

            Log.d("Success", "Read");
    }catch (IOException e){
            Log.e("Failed", "Could not load '" + e.getMessage()+ "'!");
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号