【问题标题】:Write file on sdcard from AudioSystem (C++ code) in Android 5.0.1_r1在 Android 5.0.1_r1 中从 AudioSystem(C++ 代码)在 sdcard 上写入文件
【发布时间】:2015-03-07 21:02:34
【问题描述】:

我正在尝试从 AudioSystem.cpp 在 Android 设备的 SD 卡中写入文件。这是 Android 框架中的一项服务。我正在修改源代码。

我正在使用以下代码:

    int fd = open("/sdcard/file.txt", O_RDWR | O_APPEND | O_CREAT, 0666); 
    if(fd > -1) { 
     __android_log_print(ANDROID_LOG_DEBUG, "gxp18", "Writing to SDCARD");
         write(fd, "1", strlen("1")); 
         write(fd, "\n", 1); 
         close(fd); 
    } 
    else __android_log_print(ANDROID_LOG_DEBUG, "gxp18", "ERROR while writing to SDCARD fd = %d", fd);

通过运行代码,我总是得到 fd = -1

原代码如下:

status_t AudioSystem::startInput(audio_io_handle_t input,
                                 audio_session_t session)
{
    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();

    if (aps == 0) return PERMISSION_DENIED;
    return aps->startInput(input, session);
}

修改后的是:

status_t AudioSystem::startInput(audio_io_handle_t input,
                                 audio_session_t session)
{
    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();

    /* start */

    int fd = open("/sdcard/file.txt", O_RDWR | O_APPEND | O_CREAT, 0666); 
    if(fd > -1) { 
     __android_log_print(ANDROID_LOG_DEBUG, "gxp18", "Writing to SDCARD");
         write(fd, "1", strlen("1")); 
         write(fd, "\n", 1); 
         close(fd); 
    } 
    else __android_log_print(ANDROID_LOG_DEBUG, "gxp18", "ERROR while writing to SDCARD fd = %d", fd);

    /* end */

    if (aps == 0) return PERMISSION_DENIED;
    return aps->startInput(input, session);
}

【问题讨论】:

  • 在清单文件中请求 WRITE_EXTERNAL_STORAGE 权限?
  • 什么清单文件?这不是一个 Android 应用程序,它是 Android 框架 (AOSP) 的一部分。我正在修改源代码。
  • 请展示未修改的源代码。
  • 嗯...我希望原始代码将文件写入 sdcard。但那不是我看到的。在这个低级别上,它可能是一个权限问题。 getlasterror() 的_lasterror, _error, _errno 说明了什么?会有一些信息。

标签: android c++ system-calls android-framework


【解决方案1】:

尝试写信给/storage/emulated/0/file.txt 自 JB 以来,存储目录发生了一些变化。 我假设您是通过系统服务这样做的

【讨论】:

  • 我正在使用 Nexus 5 设备来测试我的代码。在这种情况下,sdcard 的路径是 /storage/sdcard0/file.txt。我使用 adb 仔细检查了它。不幸的是,我仍然得到 fd = -1
猜你喜欢
  • 1970-01-01
  • 2015-09-22
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多