Skip to content Skip to sidebar Skip to footer

Imread Works In A Normal C++ Program, But Not In Android With Ndk Native

#include #include #include using namespace cv; #include extern 'C' JNIEXPORT jstring JNICALL Java_com_ex

Solution 1:

Android directories do not match Linux at all.

A good solution is to find your file through Java and pass the path to the file as a parameter to function Java_com_example_cppinandroid_MainActivity_stringFromJNI.

If your file is in public files, you can try "/sdcard/a.png" - but the path may vary between different android shells.

The possible solution from reading from public files:

Java:

Filef=newFile(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + fileName);
if (f.exists()) {
foo(f.getAbsolutePath());

NDK:

extern"C" JNIEXPORT jstring JNICALL
Java_com_example_cppinandroid_MainActivity_stringFromJNI(JNIEnv* env,
                            jobject, jstring javaPath)
{
    constchar* path = env->GetStringUTFChars(javaPath, 0);
    Mat image;
    image = imread( path, 1 );
    env->ReleaseStringUTFChars(javaPath, path );
    if ( !image.data )
    {
        return env->NewStringUTF( "No data in image!" );
    } else
    {
        return env->NewStringUTF( "Data is in image" );
    }
}

A better way to store a picture in assets and read AAssetManager like Android read text file from asset folder using C (ndk) or How to properly pass an asset FileDescriptor to FFmpeg using JNI in Android.

Post a Comment for "Imread Works In A Normal C++ Program, But Not In Android With Ndk Native"