从文件路径返回位图的JNI方法

时间:2016-04-25 06:33:00

标签: android c java-native-interface

下面是我的代码,用于从in参数指定获取byte [],但我想获得路径所代表的位图不仅仅是字节数组,这样我就不必将它转换为java中的位图。 / p>

JNIEXPORT jbyteArray  JNICALL Java_com_lightbox_android_photoprocessing_PhotoProcessing_getFile(JNIEnv *env, jobject thisObj,jstring string)
{
    char * path;

    path = (*env)->GetStringUTFChars( env, string , NULL ) ;
    char* buffer = 0;
    long length;
    FILE * f = fopen (path,"r"); //was "rb"

    if (f)
    {
        fseek (f, 0, SEEK_END);
        length = ftell (f);
        fseek (f, 0, SEEK_SET);
        buffer = (char*)malloc ((length+1)*sizeof(char));
        if (buffer)
        {
            fread (buffer, sizeof(char), length, f);
        }
        fclose (f);
    }
    buffer[length+1] = '\0';


    int size = length+1;
    jbyteArray array = (*env)->NewByteArray(env, size);
    (*env)->SetByteArrayRegion(env, array, 0, size,buffer);
    free(buffer);
    return array;
}

0 个答案:

没有答案