androidc++stb-image

stbi_load() return NULL and "PNG not supported: unknown PNG chunk type" error occurred, when load android internal storage file path


Purpose

I am making an Android application with vulkan. I saved bitmap in "texture.png" file in the device internal storage. I want to show this image with vulkan. Then I need to pass image data to c++ somehow.

Try

I tried below process:

Error

But stbi_load() return NULL and stb_error message is "PNG not supported: unknown PNG chunk type".

How to fix to load image in android internal storage file?

code

outlet of my code.

// create file path (kotlin)
val filePath: String = File(context.filesDir, "texture.png").path
// load image file (c++)

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

void loadImage(const std::string filePath) {// same as ↑ filePath

    int width, height, channels;
    auto* pImage = stbi_load(filePath.c_str(), &width, &height, &channels, 0);

    if (pImage == nullptr) {
        return;// I'm here.
    }
    // (some successive process)
}

Solution

  • I found the alternative to pass image data to c++.

    before

    I tried and give up this.

    after

    It is working very well. I can show the file image with vulkan now.

    code outlet

    
    JNIEXPORT void JNICALL
    Java_com_example_yourproject_NativeAPI_loadBitmap(JNIEnv* env, jobject thiz, jobject bitmap) {
        AndroidBitmapInfo androidBitmapInfo;
        AndroidBitmap_getInfo(env, bitmap, &androidBitmapInfo);
        void* pixels;// image data.
        AndroidBitmap_lockPixels(env, bitmap, &pixels);
        AndroidBitmap_unlockPixels(env, bitmap);
        
        ・
        ・
        ・
        // For example, copy image data to vulkan image memory.
        void* p;
        vkMapMemory(device, imageMemory, 0, VK_WHOLE_SIZE, 0, &p);
        memcpy(p, pixels, width * height * sizeof(uint32_t));
        vkUnmapMemory(device, imageMemory);
    }
    

    Don't forget to link jnigraphics in CMakeList.txt!