0

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.

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..");
    }

yes i got the solution replace the parameter bmpArray into bmp

i.e

 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..");

    }
  • Why? You can do this directly from Java. Why do you think you need JNI? – user207421 Jan 04 '14 at 09:18
  • @EJP it little bit faster, realy. –  Jan 04 '14 at 10:50
  • Is it really? Do you have any evidence for that claim? Or are you just guessing? You have one JNI boundary to cross and at least three callbacks back to Java in the code you've posted. Are you aware of the costs of those operations? – user207421 Jan 04 '14 at 12:04
  • How can you possibly have tested it when you can't get it working and have to ask a question here about how to do so? – user207421 Jan 04 '14 at 17:39
  • @challa can you convert bytearray to Bitmap instead of saving and return bitmap? – Prateek Yadav Apr 25 '16 at 06:27

1 Answers1

2

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Question was: "How to write byte array data in to .jpg file in android using JNI". Pls keep your advices with you... –  Jan 06 '14 at 12:13
  • 1
    Sure. Don't forget to let us know how much faster it is your way to justify all the extra cost and complication. – user207421 Jan 06 '14 at 16:42
  • Omg..If I have all hard working C code (conversion, modification,...), it will be faster to write results to file in this C file. –  Jan 22 '14 at 10:51
  • @user2556562 None of that was mentioned in your question, or when I asked you why you wanted to use JNI, several hours before I posted this answer. The code in this answer is fully equivalent to the code you posted, except that it works. I answered the question you asked. Still waiting to hear how much faster it is via JNI, 3.5 years later. – user207421 Sep 07 '17 at 22:05