我试图使用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;
}返回类型为double的原因是,我计划将该函数用于另一个目的,但我首先尝试的是io文件。在logcat中,我得到以下错误:
Fatal signal 11: (SIGSEGV) at 0x00000000c (code = 1) .....有趣的是,当我尝试使用java文件io在调用者活动中写入或读取同一个文件时,我没有任何问题。并确保我在清单文件中有写权限。另外,如果我移除FILE* file ...行,并尝试使用函数返回的双值,则程序运行良好。有人能帮忙吗?例如,问题是否应该包括在Android.mk或Application.mk文件中?
更新我使用三星星系注释2,如果这有什么区别的话
UPDATE2
下面是完整的代码:
java活动:
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>更新
上一次更新:我在其他人的手机上尝试了同样的代码,也是星系注释2,它起了作用。好像是我的手机出了问题。叹息
发布于 2013-03-07 09:05:36
也许文件路径是错的。我使用SDcard路径作为"/mnt/sdcard/“。
我记不起来了,但你可以试试“/mnt/sdcard/.”或者“mnt/sdcard/.”。
或者,您可以使用SDCard获得Environment.getExternalStorageDirectory().getAbsolutePath()路径。
https://stackoverflow.com/questions/15263982
复制相似问题