Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Make sure that you pass <strong>a valid file name</strong> and <strong>writable directory path</strong> to fopen.</p> <p>I was getting </p> <blockquote> <p>Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1) ...</p> </blockquote> <p>because fopen was trying to open a directory instead of a file.</p> <p>Check the logs in LogCat from Eclipse or using adb logcat for other possible errors.</p> <p>I've tested your code with the following modifications and it works.</p> <p><strong>MainActivity.java</strong>:</p> <pre><code>public class MainActivity { private static final String TAG = MainActivity.class.getName(); static { try { System.loadLibrary("mynativelib"); } catch (UnsatisfiedLinkError ex) { Log.e(TAG, "WARNING: Could not load native library: " + ex.getMessage()); } } public static native int DownloadFile(String downloadDirectoryPath); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int res = DownloadFile(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + Environment.DIRECTORY_DOWNLOADS + File.separator + "test.jpg"); Log.d(TAG, "Result Code: " + res); } } </code></pre> <p><strong>main-jni.cpp</strong></p> <pre><code>#include &lt;jni.h&gt; #include &lt;android/log.h&gt; #include &lt;string&gt; #include &lt;curl/curl.h&gt; #define LOG_TAG "native" #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) #define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) #define LOG_WARN(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) #define LOG_DEBUG(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) #ifdef __cplusplus extern "C" { #endif // [FIX for Android 4.2.x] // "WARNING: Could not load native library: // Cannot load library: soinfo_relocate(linker.cpp:975): cannot locate symbol "__exidx_end" referenced by" // http://stackoverflow.com/a/14501998/313113 void __exidx_start() { } void __exidx_end() { } size_t write_data(void *ptr, size_t size, size_t count, FILE *stream) { size_t written; written = fwrite(ptr, size, count, stream); LOG_DEBUG("Writing data to file stream %u", written); return written; } jint Java_com_company_awesomeapp_MainActivity_DownloadFile(JNIEnv* env, jobject thiz, jstring downloadDirectoryPath) { CURLcode res; res = curl_global_init(CURL_GLOBAL_ALL); jint temp = 3; LOG_DEBUG("Downloading file"); const char *nativeDownloadDirPath = env-&gt;GetStringUTFChars(downloadDirectoryPath, 0); LOG_DEBUG(nativeDownloadDirPath); CURL *curl; FILE *fp; std::string url = "http://travel.paintedstork.com/blog/wp-content/uploads/2012/10/2013-calendar-images-1.jpg"; curl = curl_easy_init(); if (curl) { fp = fopen(nativeDownloadDirPath, "wb"); res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); LOG_DEBUG("Before write function"); res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); LOG_DEBUG("After write function"); LOG_DEBUG("Before write data"); res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); LOG_DEBUG("After write data"); res = curl_easy_perform(curl); curl_easy_cleanup(curl); if (fp) fclose(fp); } env-&gt;ReleaseStringUTFChars(downloadDirectoryPath, nativeDownloadDirPath); return res; } /** * The VM calls JNI_OnLoad when the native library is loaded (for example, through System.loadLibrary). * JNI_OnLoad must return the JNI version needed by the native library. * * @see http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html#JNI_OnLoad */JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env; if (vm-&gt;GetEnv(reinterpret_cast&lt;void**&gt;(&amp;env), JNI_VERSION_1_6) != JNI_OK) { return -1; } // [*] Get jclass with env-&gt;FindClass. // [*] Register methods with env-&gt;RegisterNatives. //jniRegisterNativeMethods(env, "dev/android/sample/AndroidNDKSampleActivity", sMethods, NELEM(sMethods)); return JNI_VERSION_1_6; } /** * The VM calls JNI_OnUnload when the class loader containing the native library is garbage collected. * This function can be used to perform cleanup operations. * * Because this function is called in an unknown context (such as from a finalizer), * the programmer should be conservative on using Java VM services, and refrain from arbitrary * Java call-backs. * @see http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html#JNI_OnUnload */JNIEXPORT void JNI_OnUnload(JavaVM *vm, void *reserved) { } #ifdef __cplusplus } #endif </code></pre> <p>The file will be saved at: /storage/emulated/0/Download/test.jpg</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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