简体   繁体   中英

Asset Manager has issues with my file(Android)

I have a file called Gate.IC inside my assets in my Android App.

I use this code to measure the length of the file in the assets:

private byte[] Buf = new byte[1024*512];

public int FileLength (String s)
{
    int Count = 0;
    try {
        InputStream s2 = assetManager.open(s);
        int tmp = 0;
        while ((tmp=s2.read(Buf))>0)
            Count+=tmp;
        s2.close();
    }
    catch (IOException e) {
        String Message = e.getMessage();
    }
    return Count;
}

This code works fine for all files except this one.

When it gets to this file, it does open it(and shows the correct file length), but when it reads it I get an IOException and the LogCat says "Error reading asset data" and then "Unable to access asset data: -1"

If I take a different file and change it's name to Gate.IC and don't have the actual Gate.IC file in the assets, it works. If I change the name of the original Gate.IC into another asset's name, then I get the same error with the "cover" name.

I don't know what it is in this file that it just can't read it.

Here is the Rogue file:

https://dl.dropbox.com/u/8025882/RPG/Gate.IC

您可以使用它来获取文件的长度:

getAssets().openFd( "filename" ).getLength();

I have solved the issue. Well as I mentioend, it turns out ADT or the Android SDK packaging would compress some fo the assets. My file being my own custom format would be compressed. Once your file is compressed you cannot read it the same way I did.

There is a program in Android SDK called aapt.exe. It does the packaging of the assets. All you need to do is call this command with the flag -0 . Sounds simple, right?

The issue is that eclipse does not let you add flags to this command from within the ADT plugin.

You need to either edit the Android SDK XML build files, or to replace aapt.exe with your own program that calls the original aapt.exe program with the flags you want.

I did the latter.

Here is my devblog entry about it.

http://pompidev.net/2012/10/27/unable-to-access-asset-data-1-and-compressed-assetsandroid/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM