why lstat() not work in the fuse-mounted directory? - android

I have a directory mounted by fuse,and i am intented to use lstat() against this dirctory.But,when i run the following code,it just wait there and prompt nothing.
And by the way,i run the fuse in the android emulator.
the code is:
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!");
char *path="/data/pwrite/test_12/";
struct stat *stbuf;
int res=12;
res=lstat(path, stbuf);
printf("%d",res);
}
And,"/data/pwrite/test_12/" is the fuse-mounted directory.What's more,when i try another dicrtory that share the same parent directory but not mounted by fuse,like /data/pwrite/test_13/,it works!
so,i definely sure it is leaded by fuse.But,i'm even more confused that whether it is due to the conflict between fuse and android.
Any idea?thx

Try without the bugs and see if that works better.
#include <sys/stat.h>
#include <stdio.h>
void main(){
printf("new test!!!\n");
const char *path="/data/pwrite/test_13/";
struct stat stbuf;
int res=12;
res=lstat(path, &stbuf);
printf("%d\n",res);
}

why it not work?because there runs several fuse-deamon currently.

Related

How to find out the full directory of a file in Android phone to be used with fstream?

I am trying to test a very basic c++ code in CppDroid app to create a text file on my Android phone (Huawei P20pro). But it doesn't seem to work. I think the problem is with the file directory. Can someone let me know how to get the real full directory of a file/folder in Android phone?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream NewFile;
NewFile.open("/Internal Storage/CppDroid/projects/project_dec23a/Gyrocopter.txt");
NewFile << "This is a file about a gyrocopter. \n";
NewFile.close();
return 0;
}

Failed to read files in NDK C++ using fstream and ifstream

I am trying to load a file in NDK using ifstream but it fails to read it. I've double checked the path of the file and it is not wrong. The same program works in normal C++(without the JNI stuff ofcourse).
#include <jni.h>
#include <string>
#include <iostream>
#include <istream>
#include <sstream>
#include <fstream>
using namespace std;
extern "C"
{
JNIEXPORT jstring JNICALL Java_com_example_aaaaatrytest_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */) {
string file_path = "/home/moe/Desktop/blah.txt";
std::ifstream fim(file_path);
if(fim.is_open())
{
string pass = "File Loaded";
return env->NewStringUTF(pass.c_str());
}
else{
std::string fail = "Failed to load file";
return env->NewStringUTF(fail.c_str());
}
}
}
After removing if-else and debugging, this is what debugger displays:
SIGTRAP (signal SIGTRAP)
env = {JNIEnv * | 0x55bc7ccc00} 0x00000055bc7ccc00
{jobject | 0x7fcefb1af4} 0x0000007fcefb1af4
I have tried to use fstream instead of ifstream but same error. I've also provided external storage write and read permission in manifest.xml but it didn't help.
This problem is format independent as I've tried to put different files in the path. Why is it failing to read the file?
l have copied the file to my device and gave android path but it still fails to read. My android's path to file looks like this "/storage/emulated/0/abc/abc.txt".
Your app needs READ_EXTERNAL_STORAGE permission.
Here is a small snippet that helps to request this permission at runtime: see READ_EXTERNAL_STORAGE permission is in manifest but still doesn't work.

How to make Android application from from C++ program using OpenCV

I have a C++ program that starts like this:
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>
#include "linefinder.h"
#define PI 3.1415926
using namespace cv;
int main(int argc, char* argv[]) {
int houghVote = 200;
string arg = argv[1];
bool showSteps = argv[2];
string window_name = "Processed Video";
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
VideoCapture capture(arg);
I want to make an Android app from it.
I have NDK installed but I don't know what I have to do now.
Do I have to change anything in the C++ main program so that it will run on Android, or can I compile and run it unchanged?
Introduction to Android
Android provides a rich application framework that allows you to build innovative apps and games for mobile devices in a Java language environment. ... Android apps are built as a combination of distinct components that can be invoked individually.
You need to construct your app using Java, then call your C++ code via JNI.
Also, Android NDK has this to say
you should only use the NDK if it is essential to your app—never because you simply prefer to program in C/C++
In particular, read Using the NDK to understand why you can't run your C++ main program without changing anything.

Eclipse CDT code analysis disagrees with GCC - why?

I like to build the Android NDK example "native-audio", supplied with the NDK.
The NDK-supplied compiler swallows it without complaint, but Eclipse is showing several errors in file native-audio-jni.c.
In the code excerpt below, all of the static variable declarations have red underlining, because Eclipse claims that the types cannot be resolved. They are defined right up there in the OpenSLES.h file, which Eclipse does find if I Ctrl + left click the file.
Note that I modified the lines where hello_clip and android_clip are included compared to the example source - I put the array declarations in those "header" files also (they contained just comma separated data), since Eclipse had a problem parsing the syntax, too:
static const char array[] = #include"blah.h";
I tried things suggested over the internet, like starting with eclipse.exe -clean, clean project / rebuild, refresh project, etc., none helps.
So, what could be the reason/solution for Eclipse not resolving those types?
#include <assert.h>
#include <jni.h>
#include <string.h>
// for __android_log_print(ANDROID_LOG_INFO, "YourApp", "formatted message");
// #include <android/log.h>
// for native audio
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
// for native asset manager
#include <sys/types.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
// pre-recorded sound clips, both are 8 kHz mono 16-bit signed little endian
// includes data arrays here
#include "hello_clip.h"
#include "android_clip.h"
// engine interfaces
static SLObjectItf engineObject = NULL;
static SLEngineItf engineEngine;
// output mix interfaces
static SLObjectItf outputMixObject = NULL;
static SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
(the file is full of many more of those problems)

Auto run code when Android starts up?

I want my code will run automatically when the android starts up.And,i know that the init.rc srcipt will be runned when the android starts up.So,i decided to add my code's information as the service item in the init.rc.But unfortunately, it fails.
Below are the steps that I used:
1. I build the server.c via the ndk's cross-compiler:
server.c:
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
void sys_log(char* msg) {
FILE *fp;
time_t t;
if((fp=fopen("/data/server.log","a")) >=0)
{
t=time(0);
fprintf(fp,"%s at time: %s\n", msg, asctime(localtime(&t)) );
}
fclose(fp);
}
int main() {
printf("server begin!");
sys_log("server begin!");
)
I add the following lines to the end of /init.rc:
service socketserver /data/server
oneshot
I restart the android device
I use the instruction cat /data/server.log to check the log, but there is nothing in the server.log!
I run the server directly. Everything runs okay. Both the log and the console will prompt what i want!
So in conclusion, I think the init.rc does not start the server that I built.
Are my steps correct? Any ideas? Thank you for your help in advance!

Categories

Resources