Android:使用NDK从SD卡文件IO

时间:2013-03-07 05:45:15

标签: 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+");
    fputs("HELLO WORLD!\n", file);
    fclose(file);
return hub;
}

返回类型为double的原因是我计划将该函数用于其他目的,但我首先尝试文件io。在logcat中,我收到以下错误:

Fatal signal 11: (SIGSEGV) at 0x00000000c (code = 1) .....

有趣的是,当我尝试使用java文件io写入或读取调用者活动中的同一文件时,我没有任何问题。我确保我在清单文件中有写权限。此外,如果我删除FILE* file ...行,并尝试使用函数返回的double值,程序运行正常。有人可以帮忙吗?问题可能是我应该包含在Android.mk或Application.mk文件中,例如?

更新 我正在使用三星Galaxy note 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,它有效。似乎问题出在手机上。 叹息

1 个答案:

答案 0 :(得分:0)

可能文件路径错误。我使用SDcard路径为“/ mnt / sdcard /”。

我记不起来了,但你可以尝试“/ mnt / sdcard / ...”或“mnt / sdcard /...”。

或者,您可以使用Environment.getExternalStorageDirectory().getAbsolutePath()获取SDCard路径。