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

    }
5
  • Why? You can do this directly from Java. Why do you think you need JNI? Commented Jan 4, 2014 at 9:18
  • @EJP it little bit faster, realy. Commented Jan 4, 2014 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? Commented Jan 4, 2014 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? Commented Jan 4, 2014 at 17:39
  • @challa can you convert bytearray to Bitmap instead of saving and return bitmap? Commented Apr 25, 2016 at 6:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Question was: "How to write byte array data in to .jpg file in android using JNI". Pls keep your advices with you...
Sure. Don't forget to let us know how much faster it is your way to justify all the extra cost and complication.
Omg..If I have all hard working C code (conversion, modification,...), it will be faster to write results to file in this C file.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.