Android:使用NDK从SD卡进行文件IO

10

我正在尝试使用 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;
}

返回类型是双精度浮点型的原因是我计划将该函数用于其他目的,但我首先尝试文件I/O。在日志中,我得到以下错误:

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;
}

以下是 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的手机上尝试了相同的代码,它可以正常运行。看来问题出在我的手机上。


这是直接在<manifest>下面,你的代码没有任何奇怪的操作,并且SD卡实际上已经挂载,你确保在Java层传递的是相同的路径吗? - auselen
如果Java代码工作正常,为什么要试图使用本地代码呢?瓶颈几乎肯定是硬件问题,无论您使用Java还是C,速度都不会更快。 - Peter Lawrey
我正在编写一个应用程序,使用读取的数据点运行优化。从我编写的桌面应用程序等效版本中可以看出,在C中运行优化要快得多。在Java中,需要大约40分钟,在C中只需要不到10分钟。在手机上,性能比较是否相当呢?如果我在Java中读取数据,我的选择是在Java中进行优化,但这会慢得多,或者将数据传递给本地函数,这显然是一个坏主意。 - user1023420
user1023420,你能否在手机上安装一些低级文件浏览器,并检查顶层文件夹/中是否有sdcard?或者在终端上运行ls -l /,分别在你的手机和其他手机上运行。 - osgx
我们在各种三星(仅限三星)设备上都遇到了完全相同的问题:S5、S4和Note 4(所有设备都运行在Lollipop系统下)。 - Dmitry Zaytsev
显示剩余10条评论
1个回答

0

可能文件路径不正确。我使用SD卡路径为“/mnt/sdcard/”。

我记不清了,但你可以尝试“/mnt/sdcard/…”或“mnt/sdcard/…”。

或者,你可以使用Environment.getExternalStorageDirectory().getAbsolutePath()获取SD卡路径。


请看上面的更新,似乎是我的手机出了故障 :/ - user1023420
或者使用Environment.getExternalStorageDirectory().getAbsolutePath()获取sdcard路径。不行,原因如下:(太糟糕了,他们关闭了这个问题)- user1023420 - 这可能不是你的手机...如果你有"/storage/sdcard0"三星声称我们必须添加"/external_sd/"然后文件名 - 但我们已经验证它在不同的S3手机上工作方式不同 - 在一些手机上失败,在其他手机上不稳定地工作。 - Howard Pautz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接