I wanna print photo img of Bitmap class in java

But, when I tried that, it was printed only black.
here is my source

I changed bitmap to byteArray & move at cocos2d part.
then create ccsprite from byteArray
But that was delighted me with black screen…

How can I reslove this problem…?

In java(Cocos2dxActivity.java)

    @Override
    public void onActivityResult( int requestCode, int resultCode, Intent data ) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case REQ_CODE_PICK_IMAGE:
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    String filePath = Environment.getExternalStorageDirectory()
                            + "/temp.jpg";

                    System.out.println("path" + filePath); // logCat으로 경로확인.

                    Bitmap selectedImage = BitmapFactory.decodeFile(filePath);

                    if (selectedImage != null){
                    byte[] pixels = new byte[selectedImage.getWidth() * selectedImage.getHeight() * 4];
                    ByteBuffer buf = ByteBuffer.wrap(pixels);
                    buf.order(ByteOrder.nativeOrder());
                    selectedImage.copyPixelsToBuffer(buf);

                        mGLView.SendUserImg(pixels, selectedImage.getWidth(), selectedImage.getHeight(), selectedImage.getRowBytes());

                    }

                 }
            }
            break;
        }
    }

In cpp(main.cpp = glue)

    void Java_org_cocos2dx_lib_Cocos2dxGLSurfaceView_nativeSendUserImg( JNIEnv* env, jobject thiz, jint width, jint height, jbyteArray img, jint length )
    {
        jbyte *b = (jbyte *)env->GetByteArrayElements(img, NULL);

    SpriteAlbumData::instance()->set((unsigned char*)b, width, height, length);
    env->ReleaseByteArrayElements(img, b, 0 );
    }

In cpp(SpriteAlbumData.cpp)

void SpriteAlbumData::set( unsigned char* pData, int width, int height, int length )
{
    CCImage* img = new CCImage;
    img->initWithImageData(pData, length);
    CCTexture2D * tex = new CCTexture2D();
    tex->initWithImage(img);
    CCSprite *sprite = CCSprite::spriteWithTexture(tex);

    m_sprLoaded = Sprite::create(sprite);

    m_sprites.push_back(m_sprLoaded);
}

Why don’t you pass the URL? Just send the filePath to cocos2dx , then use cocos2dx to load the image…

How to use filePath?
I didnt know that’s information.

If u have that’s example and not annoyed, plz show me url :slight_smile:

waiter song wrote:

Why don’t you pass the URL? Just send the filePath to cocos2dx , then use cocos2dx to load the image…

May like this?

In java(Cocos2dxActivity.java)

 @Override
    public void onActivityResult( int requestCode, int resultCode, Intent data ) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case REQ_CODE_PICK_IMAGE:
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    String filePath = Environment.getExternalStorageDirectory()
                            + "/temp.jpg";

                    System.out.println("path" + filePath); // logCat으로 경로확인.
                        mGLView.SendUserImg(filePath);

                    }

                 }
            }
            break;
        }
    }

In cpp(main.cpp = glue)

 void Java_org_cocos2dx_lib_Cocos2dxGLSurfaceView_nativeSendUserImg( JNIEnv*  env, jobject thiz , jstring url )
    {
        change jstring to std::string!!
std::string s = XXXXX...

SpriteAlbumData::instance()->set(s);
    }

In cpp(SpriteAlbumData.cpp)

void SpriteAlbumData::set( std::string s )
{
    m_sprLoaded = Sprite::create(s.c_str());
    m_sprites.push_back(m_sprLoaded);
}

tytytytytytytytytytytytytytyty so muuuuuuuuuuuuuuuuuuuuuuuuuch~~

I try to use this now. and will be back soon.

I use that…
but appear the white block instead of currect img.

void SpriteAlbumData::createFromPath(const char *path)
{
    FILE* pFile = fopen(path, "rb");
    if ( pFile == NULL ) return;

    int nSize = 0;

    unsigned char * pBuffer = NULL;

    fseek(pFile,0,SEEK_END);
    nSize = ftell(pFile);
    fseek(pFile,0,SEEK_SET);
    pBuffer = new unsigned char[nSize];
    nSize = fread(pBuffer,sizeof(unsigned char), nSize,pFile);
    fclose(pFile);

    LOGD("%s", path);

    CCImage image;

    if ( !image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtJpg) )
        return;


    CCTexture2D *texture = new CCTexture2D();
    texture->initWithImage(ℑ);
    if( texture ) {
        CCSprite *sprite = CCSprite::spriteWithTexture(texture);
        m_sprLoaded = Sprite::create(sprite);
    }

    m_sprites.push_back(m_sprLoaded);
}
void SpriteAlbumData::createFromPath(const char *path)

Is the path like “xxx/xxx/xxx.jpg” ?
Why not just use the path to create sprite…
Why you try to do like this : path~~>buffer~~>CCImage~~>CCTexture2D~~>CCSprite~~>m_sprLoaded…
Just do like this: path~~>CCSprite->m_sprLoaded(CCSprite)

m_sprLoaded = Sprite::create( CCSprite::create(path) );

you should create CCTexure2D in GLThread (or the thread which EGL context created on)
try using GLSurfaceView.queueEvent to do that.

@splash huang
I would like to try the method you have mentioned

create CCTexure2D in GLThread (or the thread which EGL context created on)
try using GLSurfaceView.queueEvent to do that.
can you help me understand, how to do this? is there a guide or tutorial I can refer to?
Thanks