aabramovsky aabramovsky - 3 years ago 196
C++ Question

Why fopen fails on Nexus5x android device ( C++ )

I am trying to save a bmp into file in my C++ android app (I am working with chromium project).

FILE* fp = fopen("/myimage.bmp", "wb"); // result: fp==NULL, errno==30

FILE* fp = fopen("/Pictures/myimage.bmp", "wb"); // result: fp==NULL, errno==2

Phone is Nexus 5x having no sdcard.
are set.

Looks like I am using invalid path? Which path would be valid? It is a debug output, so I need any path that would work.

Answer Source

If you're accessing your internal storage of your application then the root path of your application storage is

String ROOT_PATH = "/data/data/" + "com.your.package.name" + "/";

So in case of accessing your file from your internal storage you have to declare the file path like this.

FILE* fp = fopen(ROOT_PATH + "myimage.bmp", "wb"); 

But this doesn't ensure your problem to be solved as @CommonsWare suggested,

"the root path of your application storage is" -- the path varies by OS version, user account (primary vs. secondary), and possibly device manufacturer. NEVER HARDCODE PATHS. If NDK code needs to write to a location, Java code should be passing in the path, where that is derived from a method (e.g., getFilesDir()).

So I'm referring to his comment again in your question.

You cannot write to / on any Linux-based system without superuser-level privileges. "I am working with chromium project" -- then figure out where Chromium writes files, and write your files there.

These are some valuable insights suggested by @CommonsWare which might help you. Thanks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download