如何使用JNI将字节数组数据写入android中的.jpg文件 [英] How to write byte array data in to .jpg file in android using JNI

查看:239
本文介绍了如何使用JNI将字节数组数据写入android中的.jpg文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在Android中使用JNI将数据的字节数组存储到.jpg文件中. 我使用以下代码,它将存储字节数组数据,但.jpg文件未打开,或者在手机画廊中打开文件时显示错误.

I want to store byte array of data in to .jpg file by using JNI in android. i use following code, it will store the byte array data but .jpg file is not open or it will show error while opening the file in phone gallery.

这里bmpArray:字节数组 filePath:sdcard目录+文件名

Here bmpArray : byte array filePath : sdcard directory+ filename

void Java_com_appsforbb_businesscardreader_ImageUtility_setNativeBitmapArray(JNIEnv* env, jclass object,jbyteArray bmpArray,jstring filePath)
{
    jbyte* bmp= env->GetByteArrayElements(bmpArray, 0);
    jsize length = env->GetArrayLength(bmpArray);
    jbyteArray arr = env->NewByteArray(length);
    const char* path = env->GetStringUTFChars(filePath, 0);
    FILE* file = fopen( path, "w+" );
    fwrite(bmpArray, 1, length, file );
    fflush(file);
    fclose(file);
    LOGI("Byte array stored..");
    }

是的,我找到了解决方案 将参数bmpArray替换为bmp

yes i got the solution replace the parameter bmpArray into bmp

 void Java_com_appsforbb_businesscardreader_ImageUtility_setNativeBitmapArray(JNIEnv* env, jclass object,jbyteArray bmpArray,jstring filePath)
    {

       jbyte* bmp= env->GetByteArrayElements(bmpArray, 0);
       jsize length = env->GetArrayLength(bmpArray);
       const char* path = env->GetStringUTFChars(filePath, 0);
       FILE* file = fopen( path, "w+" );
       fwrite(bmp, 1, length, file );
       fflush(file);
       fclose(file);
       free(bmp);
       free(file);
       LOGI("---------->byte array stored..");

    }

推荐答案

忘记JNI并在Java中完成:

Forget the JNI and do it in Java:

OutputStream out = new FileOutputStream(filePath);
out.write(bmpArray);
out.close();

如果运行速度比您发布的内容的更正版本快,我会感到惊讶.

I will be astonished if this doesn't run faster than a corrected version of what you posted.

这篇关于如何使用JNI将字节数组数据写入android中的.jpg文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆