Sprite from sdcard image

Because of the resource size limit for apps in the Android marketplace, we have to download additional resources the first time a user opens the application and write them to the sdcard. Once there I am having a difficult time opening sprites from files on the sdcard or loading plist sprite frames from locations on the sdcard. The application does have storage access, and FILE* references come back as not null. Is there anything in the version that would not allow sprites to be created from sdcard resources?

Thanks,
Jeff

I think you can use CCFileUtils::getFileData(“/sdcard/xxx”, …).

Thanks for the response! Actually, I figured out that 0.9.1 version had code that forced all file lookups to go to assets which was breaking everything. I then looked at the 0.9.2 code and realized that line had been changed so anything that starts with / (like /sdcard) wouldn’t have assets prepended. So now I’m using .9.2. The next issue I’m seeing is for sound clips, and the simple audio engine doesn’t seem to support loading resources from sdcard as I see it in the list of cocos2dx improvements. If you have any idea for a workaround for playing sounds from the sdcard, please let me know. Due to apk size restrictions, puttting them in assets/ or resources/ won’t work. Thanks again!

Yes, #721 had been created for it. It will be fixed in next version.

In case anyone is interested in loading music from the sdcard, the following change in Cocos2dx music will allow you to do so. Please make sure you enable the permissions of your app for storage access.

/**
     * create mediaplayer for music
     * @param path the path relative to assets
     * @return 
     */
    private MediaPlayer createMediaplayerFromAssets(String path){
        MediaPlayer mediaPlayer = null;

        try{    
            mediaPlayer = new MediaPlayer();
            mediaPlayer.reset();

            if(path.contains("/sdcard")) {
                mediaPlayer.setDataSource(path);
            }
            else {
                AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path);

                mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), 
                    assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());
            }

            mediaPlayer.prepare();

            mediaPlayer.setVolume(mLeftVolume, mRightVolume);
        }catch (Exception e) {
            mediaPlayer = null;
            Log.e(TAG, "error: " + e.getMessage(), e);
        }

        return mediaPlayer;
    }

How can i convert the result of CCFileUtils::getFileData to a CCSprite?

Solution:

CCImage image;
CCFileData data(imagepath.str().c_str(), "rb");
unsigned long nSize  = data.getSize();
unsigned char* pBuffer = data.getBuffer();
CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtJpg));

CCTexture2D *texture = new CCTexture2D();
texture->initWithImage(ℑ);

if( texture ) {
     CCSprite *sprite = CCSprite::spriteWithTexture(texture);
}

:slight_smile: