Java Android:使用NDK从SD卡发送IO文件
我试图使用NDK写入输出文件,但它似乎不起作用。我编写的代码如下所示:Java Android:使用NDK从SD卡发送IO文件,java,android,c,file-io,android-ndk,Java,Android,C,File Io,Android Ndk,我试图使用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+"); fp
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的原因是,我计划将该函数用于另一个目的,但我将首先尝试使用file io。在logcat中,我得到以下错误:
Fatal signal 11: (SIGSEGV) at 0x00000000c (code = 1) .....
有趣的是,当我尝试使用java文件io在调用方活动中写入或读取同一文件时,我没有遇到任何问题。我确保我在清单文件中有写权限。另外,如果我删除FILE*FILE…
行,并尝试使用函数返回的双精度值,则程序运行正常。有人能帮忙吗?例如,问题是否应该包括在Android.mk或Application.mk文件中
更新
我用的是三星galaxy note 2,如果有什么不同的话
更新2
以下是完整的代码:
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;
}
这是舱单:
<?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,并且成功了。好像是我的手机出了问题。叹息可能是文件路径错误。我使用SDcard路径作为“/mnt/SDcard/” 我记不清了,但你可以试试“/mnt/sdcard/…”或“mnt/sdcard/…”
或者,您可以使用
Environment.getExternalStorageDirectory().getAbsolutePath()
获取SD卡路径文件确实为空,但这有什么意义?如果该文件不在目录中,fopen将创建该文件,然后写入该文件。另外,正如我在问题中所说,在Main.java活动文件中使用相同的精确路径不会给我任何错误(使用file f1=new file(“/sdcard/textest.txt”)
您是否拥有android.permission.WRITE_EXTERNAL_STORAGE权限?清单文件中有一行:
。这行代码直接位于
下面,您的代码中没有做任何奇怪的事情,SD卡实际上已经挂载,您真的在Java级别通过了相同的路径吗?我上传了整个代码,并且还尝试了路径/sdcard/external\u sd/textest.txt,但效果不好,(我使用的是三星galaxy note 2)请查看上面的更新,我的手机似乎出现了故障:/“或者使用Environment.getExternalStorageDirectory().getAbsolutePath()”不,原因如下:(太糟糕了,他们关闭了这个)-用户1023420-可能不是你的手机…如果你有“/storage/sdcard0”三星声称我们必须附加“/external_sd/”,那么文件名-但我们已经验证它在不同的S3手机上的工作方式不同-在一些手机上失败,在其他手机上工作不一致
<?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>