我正在尝试使用 NDK 写入输出文件,但它似乎不起作用。我写的代码是这样的:
JNIEXPORT jdouble JNICALL Java_com_example_sdcardtest2_CppMethods_nativeCalculations (JNIEnv* env, jclass clazz, jdouble dub){
jdouble hub = dub + 10;
FILE* file = fopen("/sdcard/textTest.txt","w+");
fputs("HELLO WORLD!\n", file);
fclose(file);
return hub;
}
返回类型是双精度的原因是我打算将该函数用于其他目的,但我首先尝试使用文件 io。在 logcat 中,我收到以下错误:
Fatal signal 11: (SIGSEGV) at 0x00000000c (code = 1) .....
有趣的是,当我尝试使用 java 文件 io 在调用者活动中写入或读取同一文件时,我没有遇到任何问题。并且我确保我在清单文件中具有写入权限。此外,如果我删除该FILE* file ...
行,并尝试使用函数返回的双精度值,程序运行良好。有人可以帮忙吗?例如,我应该将问题包含在 Android.mk 或 Application.mk 文件中吗?
更新 我使用的是三星 Galaxy Note 2,如果这有什么不同的话
更新2
这是完整的代码:
爪哇活动:
public class MainActivity extends Activity {
private TextView text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.text1 = (TextView) findViewById(R.id.textView1);
double d = CppMethods.nativeCalculations(2.80);
String s = "The value of d is = " + d;
File f1 = new File("/sdcard/textTest.txt");
InputStream is = null;
try {
is = new FileInputStream(f1);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Scanner reader = new Scanner(is);
this.text1.setText(reader.nextLine().subSequence(0, s.length()));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是本机方法:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "com_example_sdcardtest2_CppMethods.h"
#include <stdio.h>
#include <string.h>
/*
* Class: com_example_sdcardtest2_CppMethods
* Method: nativeCalculations
* Signature: (D)D
*/
JNIEXPORT jdouble JNICALL Java_com_example_sdcardtest2_CppMethods_nativeCalculations
(JNIEnv* env, jclass clazz, jdouble dub){
jdouble hub = dub + 10;
FILE* file = fopen("/sdcard/textTest.txt","w+");
fputs("hello, world\n", file);
fclose(file);
return hub;
}
这是Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := com_example_sdcardtest2_CppMethods.c
LOCAL_MODULE := com_example_sdcardtest2_CppMethods
include $(BUILD_SHARED_LIBRARY)
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sdcardtest2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sdcardtest2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
更新
最后更新:我在别人的手机上尝试了相同的代码,也是 Galaxy Note 2,它成功了。好像问题出在我的手机上。叹