I've managed to compile android from source and install on my phone. I should probably say from the outset I'm not a c++ developer but I had hoped I would be able to work out how to output the data held in a variable to a file.
After a bit of googling I came up with the following code:
#include <iostream>
#include <fstream>
using namespace std;
ofstream myfile;
myfile.open ("/data/wav.raw");
myfile < mMixBuffer;
myfile.close();
frameworks/base/services/audioflinger/AudioFlinger.cpp: In member function 'virtual bool android::AudioFlinger::MixerThread::threadLoop()':
frameworks/base/services/audioflinger/AudioFlinger.cpp:1769: error: 'ofstream' was not declared in this scope
frameworks/base/services/audioflinger/AudioFlinger.cpp:1769: error: expected ';' before 'myfile'
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: 'myfile' was not declared in this scope
after this didn't work I tried which again gave compilation errors:
int fd;
fd = ::open("/data/wav.raw",O_WRONLY | O_APPEND);
::write(fd,mMixBuffer,minBufferSize);
::close(fd);
frameworks/base/services/audioflinger/AudioFlinger.cpp: In member function 'virtual bool android::AudioFlinger::MixerThread::threadLoop()':
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: '::open' has not been declared
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: 'O_WRONLY' was not declared in this scope
frameworks/base/services/audioflinger/AudioFlinger.cpp:1770: error: 'O_APPEND' was not declared in this scope
If it's not clear from the above I'm trying to append the contents of variable mMixBuffer with size minBufferSize to a file /data/wav.raw. Does anyone have any idea how I can achieve this?
/data/ directory can not be modified. You won't be having the permissions for the same. Try /mnt/sdcard (or) /data/data/.
Related
Im trying to build an Ubuntu Touch tree, based on an Aosp tree I already correctly built.
It fails with this error
CAPEWrapper.cpp:16: error: undefined reference to '__xlog_buf_printf'
this is the header that file includes
#include "CAPEWrapper.h"
which on cascade includes
#include <cutils/xlog.h>
which in turn defines
#if defined(__cplusplus)
extern "C" {
#endif
int __xlog_buf_printf(int bufid, const struct xlog_record *rec, ...);
#if defined(__cplusplus)
}
#endif
I suspect that my g++ doesn't set __cplusplus macro. Could it be a realistic scenario with this kind of error? If this could be the problem, should I need to specify a standard implementation with "stdc=something" to solve it?
Any other idea is welcome.
Make sure that your project is linking libcutils, and that it's linking it in the correct order (i.e. that -lcutils appears in the linker command line after any module that depends on it).
In the end, I found that the modules was listed inside a macro called LOCAL_WHOLE_STATIC_LIBRARIES, that in Android environment passes its content to the --whole-archive flag of GCC linker.
How to expose an API in Android kernel? Somehow the following does not work:
I did EXPORT_SYMBOL(module1_func) in module1.c
Did extern module1_func(); in header file module1.h (placed the header file in 'kernel/include/linux')
In module2.c, I did #include <linux/module1.h> and called the function module1_func()
FYI: module1.c is in kernel/security/<clean> AND module2.c is in kernel/mm
After compilation I am getting the following linker error:
kernel/mm/built-in.o: In function `get_param':
:(.text+0x75c0): undefined reference to `module1_func'
make: *** [.tmp_vmlinux1] Error 1
Btw, I did a clean build, still the same.
Instead of extern module1_func(), only declare module1_func() in module1.h.
Refer this basic module for EXPORT_SYMBOL() understanding.
I have an Android application that use JNI. In JNI I used C++ STL library to output some data into a text file.
This is the snippet code.
#include <iostream>
#include <fstream>
float mydata[4] = {0.0f};
ofstream file;
file.open("Data.txt", ios::app);
// For example
file<<mydata[1];
file.close();
when I ran ndk-build, I did not face any errors and the program seems run properly. But I did not found any text file in my Application directory.
does anybody know what could be the issue? Please help.
Thanks
Your code works, but you apparently try to access a path which is forbidden.
Firstly, be sure that your Android application asks for storage permissions.
Secondly, try to replace "Data" by an absolute path you know you have access to (e.g. "/sdcard/Pictures/Data.txt").
Also, as a better practice, you should test if the file has been opened before writing to it.
android is a kind of linux,and it must support the posix.But,when it seems not support the syscall,open().
Here is the code for testing,and i compile it via NDK:
#include <unistd.h>
#include <stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void main(){
int fd;
char pathname[128] = "/data/pwrite.txt";
fd = open(pathname, O_WRONLY);
if(fd==-1){
printf("open fail.\n");
}
perror("/data/pwrite.txt");
}
and the following is the prompt that comes from android:
kaiwii#ubuntu:~$ adb shell /data/pwrite/test1
open fail.
/data/pwrite.txt: No such file or directory
I think that the problem is in the flags - you only use O_WRONLY. But if a file does not exist you should also create it using O_CREAT flag. So if a file does not exist you should call:
fd = open(pathname, O_WRONLY | O_CREAT);
I think the problem is not the syscall open() but the fact that your are trying to access /data. This folder is accessible only for rooted mobiles or in the emulator. Have you tried to put the file into the /sdcard folder?
Currently I am working with the Android NDK and JNI. I am trying to build a C++ code with NDK.
But I got the following errors:
E:/Android/Tranining_workspace/BackUpMigrant/jni/ReadBackupArc5/ReadBackupArc5.cpp:10:19: error: fstream: No such file or directory
E:/Android/Tranining_workspace/BackUpMigrant/jni/ReadBackupArc5/ReadBackupArc5.cpp:20: error: 'ifstream' does not name a type
E:/Android/Tranining_workspace/BackUpMigrant/jni/ReadBackupArc5/ReadBackupArc5.cpp:21: error: 'ofstream' does not name a type
E:/Android/Tranining_workspace/BackUpMigrant/jni/ReadBackupArc5/ReadBackupArc5.cpp:22: error: 'ofstream' does not name a type
E:/Android/Tranining_workspace/BackUpMigrant/jni/ReadBackupArc5/ReadBackupArc5.cpp:34: error: 'string' was not declared in this scope
Can anyone please help me out?
I just encountered the same problem. Seems the STL is not automatically included in NDK projects by default. That also means iostream, fstream, string etc can't be used straight away. To enable them, you'll need to modify your Application.mk file. If you don't have one (it's in the <project>/jni directory), then just create a new, blank one. Add the line:
APP_STL := stlport_static
Also, also remember to include using namespace std; or equivalent, along with the usual #include <iostream> etc.
Did you remember your:
#include <iostream>
using namespace std;
definitions at the top of the file?
("using namespace std" isn't always a good idea, but that's a separate issue.)