Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid NDK: Error while opening a File written to SD card using NDK + SDK android
    primarykey
    data
    text
    <p>I am new to android and iam trying to read a file from SD card using NDK and sending that read bytes to SDK and iam writing it to the SD card using SDK (for checking).. I could sucefully make a call from NDK - SDK with the bytes and i could write it to the SD card using SDK.. But i coulnt open the file written to the sd card..</p> <p>I am pasting my code </p> <p>Native C file </p> <pre><code>#include&lt;stdio.h&gt; #include&lt;string.h&gt; #include &lt;jni.h&gt; #include &lt;stdlib.h&gt; #include &lt;android/log.h&gt; #include "com_example_fileupload_NativeLib.h" #define LOG_TAG "testjni" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) JavaVM *m_vm; jobject obj; jclass gFilePermInfoClass; jmethodID gFilePermInfoClsConstructor; jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env; LOGI("JNI INIT"); return JNI_VERSION_1_6; } JNIEXPORT jint JNICALL Java_com_example_fileupload_NativeLib_loop (JNIEnv *env, jobject obj) { LOGI("JNI work !"); jbyteArray data; jbyteArray data_rem; jclass clazz = (*env)-&gt;FindClass(env,"com/example/fileupload/NativeLib"); if (clazz == 0) { LOGI("FindClass error"); return 2; } gFilePermInfoClass = (*env)-&gt;NewGlobalRef(env, clazz); LOGI("JNI before jmethodID"); jmethodID javamethod = (*env)-&gt;GetMethodID(env,gFilePermInfoClass, "callFromCPP", "([B)V"); gFilePermInfoClsConstructor = (*env)-&gt;NewGlobalRef(env, javamethod); if (javamethod == 0) { LOGI("GetMethodID error"); return 3; } LOGI("JNI before calling java "); LOGI(" before File "); FILE *fd = NULL; char buff[1*1024*1024]; char *h; unsigned int nread = 0; unsigned int nread_remaining = 0; int count=0; memset(buff, '\0', sizeof(buff)); fd = fopen("/sdcard/20MB.zip","r+"); fseek ( fd, 0 , SEEK_END ); int fileSize = ftell(fd); LOGI("****SUDARSHAN C SIZE %d",fileSize); if(NULL == fd) // Check for error { LOGI("\n File opening failed\n"); return 1; } nread = (1*1024*1024); // Hard code the number of elements to 50 if(nread_remaining = fileSize%nread) { count = fileSize/nread; LOGI("***** IN IF rem %d",nread_remaining); } else{ count = fileSize/nread; LOGI("***** IN ELSE COUNT %d",count); } data = (*env)-&gt;NewByteArray(env, nread); data_rem = (*env)-&gt;NewByteArray(env, nread_remaining); if(data == NULL){ LOGI("No memory could be allocated for buffer"); return -1; } LOGI("Before While in C "); int count_test=0; int i; for(i=count;i&gt;0;i--) //while(count_test) //while( fread(buff, 1, nread, fd) ) { fread(buff, 1, nread, fd); LOGI("INSIUDE WHILE SUDARSHAN **** %d",i); (*env)-&gt;SetByteArrayRegion(env, data, 0, nread, buff); (*env)-&gt;CallVoidMethod(env, obj, gFilePermInfoClsConstructor, data); } if(nread_remaining){ char buffer[nread_remaining]; fread(buffer, 1, nread_remaining, fd); (*env)-&gt;SetByteArrayRegion(env, data_rem, 0, nread_remaining, buffer); (*env)-&gt;CallVoidMethod(env, obj, gFilePermInfoClsConstructor, data_rem); } if(feof(fd)) { printf("\n Seems like end of file was reached\n"); } else if(ferror(fd)) { printf("\n Some errors on this stream occurred\n"); } // Reset the buffer with NULLs memset(buff, '\0', sizeof(buff)); return (10+11); } </code></pre> <p>NativeLib.java</p> <pre><code>package com.example.fileupload; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.os.Environment; public class NativeLib { public int count=0; File someFile = null; FileOutputStream fos = null; File sdDir = new File(Environment.getExternalStorageDirectory().getPath()); public NativeLib() throws FileNotFoundException{ someFile = new File(sdDir.getPath() +"/22MB.zip"); fos = new FileOutputStream(someFile); //fos.flush();fos.close(); } static { System.loadLibrary("com_example_fileupload_NativeLib"); } /** * Adds two integers, returning their sum */ public native int loop(); /** * Returns Hello World string * @throws IOException */ //public native String hello(); public void callFromCPP(byte[] buff) { count=count+buff.length; try { //count=count+buff.length; System.out.println("************ Sudarshan Inside callfromcpp try "+count+" "+buff.length); fos.write(buff); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("************ Sudarshan Inside callfromcpp catch "+e.toString()); e.printStackTrace(); } // count=count+buff.length; //System.out.println("************ Sudarshan Inside callfromcpp 1 "+buff.toString()+" "+count); } </code></pre> <p>Iam calling the native fn. <strong>loop()</strong> from my <strong>MainActivity.java</strong> and the java fn. call from native is <strong>callFromCPP(byte[] buff)</strong> and here only iam writing the byte's to SD card (named <strong>22MB.zip</strong>) returned from native..</p> <p>PLZ help</p> <p>Reagards,</p> <p>Deepak</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload