我正在尝试从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);
}
尝试写/storage/emulated/0/file.txt
自 JB 以来,存储目录发生了一些变化。我假设您是从系统服务中这样做的