c++ copy file for ios and android - android

I am familiar with objective-c to copy file using cocoatouch sdk.
But I hope add copy file function to my game (cocos2dx c++).
Is there a common way for ios and android to copy file using c++?
Your comment welcome

int main()
{
std::ifstream src("from.ogv", std::ios::binary);
std::ofstream dst("to.ogv", std::ios::binary);
dst << src.rdbuf();
}
Taken from here.
Of course in Android you need NDK for that.

Related

Binding native Android (and iOS) libraries in MAUI

We are working on an Android and iOS (but we want to just get Android working for now) application that needs to rely on some native drivers (.so, .a and some .ini files). The company providing those drivers also provides a Xamarin project to showcase how the drivers are used. They seem to be storing those driver and other files as assets (.ini files under Assets/Files and .so files under Assets/lib/arm64-v8a and Assets/lib/armeabi-v7a respectively) and extracting the former using the following code:
void ExtractAssets()
{
var assets = ApplicationContext.Assets;
var paths = assets.List("Files");
foreach (var path in paths)
{
// Read the compressed file and extract
string readPath = Path.Combine("Files", path);
byte[] buffer = new byte[32000];
int bytesRead = 0;
using (var inStream = assets.Open(readPath))
using (var outStream = ApplicationContext.OpenFileOutput(path, Android.Content.FileCreationMode.Private))
{
do
{
bytesRead = inStream.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
outStream.Write(buffer, 0, bytesRead);
}
}
while (bytesRead > 0);
}
System.Diagnostics.Debug.WriteLine(string.Format("Extracted '{0}' to '{1}'", readPath, path));
}
}
Since Xamarin is becoming obsolete and MAUI is the next big thing, we decided to build our project on that. My colleague and I have spent the entire afternoon on a sample project and we still do not manage to simply load a libhello-jni.so file (we use this one) at runtime to call a stringFromJNI(). We tried:
Using an Android Native Binding project — we never really got what that's about and how mappings are supposed to work
Trying to include the libhello-jni.so file as an asset and load it using JavaNative.LoadLibrary("hello-jni"); and this had various unsuccessful results, it either did not find the library at all or it was complaining about the lib being built for x86 time processor instead of x64.
Can anyone please help us with some sample code allowing us to just simply bind a native .so file in a MAUI project, or at least provide us with a good guide? Thank you and sorry for the long post.
Not sure if this is exactly what you are going for or not, but we use bindings to a C library that we develop for various platforms, including Android. The way to correctly place native libraries into the final APK is via the EmbeddedNativeLibrary type in .csproj as demonstrated here. This will include your native library alongside the other native libraries that the toolchain is going to create (like the .NET runtime, garbage collection native lib, etc).
One thing changed in .NET 6, however. This affects us as an SDK distributor, because we need to include all necessary files inside of Nuget packages, so it may not affect you. In Xamarin, when you embed native libraries, the native libraries are literally stitched into your assembly and no further files are needed. In .NET 6, now they are all placed into a separate .aar file which also needs to be available when building the final application.
If you do everything correctly, you will see your native library inside of the APK in the lib/<arch> directory. Our native library is not JNI, and thus we use P/Invoke so we don't need a LoadLibrary call but I am reasonably certain that the call should succeed with the libraries in this directory.

How can i reach files from Android NDK?

I never make a android project and i don't know java. I am trying to port my c++ sdl opengl project to android. I am using android studio. I opened sdl sample android project and now i can manage project from main.cpp . How do i know this because I changed the color of the window and it worked. But when i try to load shader.text file with ifstream program crashed. I understand that this will not happen. After this i searched but I couldn't find a complete code or an example.
When i put files in assets folder and build apk. apk size grows as files. Therefore I need to kepth files in assets folder ?
I think i need to use asset manager but of course i don't know.
What files should I write what code? I mean what i need to add main.cpp and I need to add code java side also ?
main.cpp :
int main()
{
//lets say i have filePath like these
char file_vert[100];
snprintf(file_vert,100,"shaders/vertexShader.text");
char file_Texture[100];
snprintf(file_Texture,100,"textures/image.jpg");
//i am using them in cpp project
//shader text file like this
std::ifstream vShaderFile;
vShaderFile.open(file_vert);
//and i am loading texture file with "stbi" library
unsigned char *data = stbi_load(file_Texture, &width, &height, &nrComponents, 0);
}

How to access cascade xml file from C++ dll in Android

I'm developing an android application that uses OpenCV C++ dll(.so file) in Unity.
The C++ dll accesses a cascade file to use OpenCV functions.
*C++ DLL side code
cv::String face_cascade_name = "haarcascade_frontalface_alt.xml";
if (!face_cascade.load(face_cascade_name)) {
printf("--(!)Error loading face cascade, please change face_cascade_name in source code.\n");
return -1;
};
However If I build an android application in Unity and install the app on Android device, it fails to load the xml file.
How can I access the cascade xml in C++ DLL in Android apk?
*If I put the xml to assets/StreamingAssets in Unity, the xml file exists in myapp.apk/assets folder.
Android is a special case since the StreamingAssets folder is compressed and unusable by the OpenCV file reader. You have to 1) copy the file from your StreamingAssets folder to the persistent data path (there's documentation on how to copy and write files in the unity manual), 2) fetch the name of that file using Application.persistantDataPath, like:
string cascadeFileName = (Application.persistentDataPath + "/" + fileName);

Qt Android: where is my file if I write to "./myfile.txt"?

I am porting my Qt desktop app to Android.
In the desktop version, I will get a myfile.txt in the same folder of the executable file when I write to ./myfile.txt.
After I ported it to Android, the app runs smoothly but I cannot find the file anywhere as I'm new to Android...
So where is the file? Is there any concept like pwd for an Android app?
I'm using QFile and QTextStream to generate and write the file.
QFile file;
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream stream(&file);
stream << "something";
Thank you!
it might be in "application storage".
/data/data/com.example.yourAppPackageName/
or in sd-card application storage
/sdcard/Android/data/com.example.yourAppPackageName/

How can I access .so file in apk at runtime?

I'd like to access(read) my .so file built in NDK at runtime in order to
check up on its hash value.
To do that, I used AAssetManager_open() function but always failed.
const char* filename = "/data/data/com.mycompany.app/lib/libmyndk.so"; // failed with this.
const char* filename = "assets/libmyndk.so"; // Also failed with this.
AAsset* asset = AAssetManager_open(mgr, (const char*) filename, AASSET_MODE_UNKNOWN);
Please let me know why it persist to fail.
I solved out this issue. We don't need to use AssetManager for just simply accessing.
just use fopen() in NDK(c/c++ files).
for example,
void initNDK()
{
const char* filename = "/data/data/com.company.app/lib/libmyndk.so";
FILE* fp = fopen(filename, "rb");
if(fp)
TRACE("Success");
else
TRACE("Failure");
}
Thank you All.
you have load .so file during application start.
By
static {
System.loadLibrary("libpacman");
}
Than you can access file .
i hope this is help you.
Have you tried:
const char* filename = "libmyndk.so";
Above all, I succeeded in reading/accessing some file in assets folder with AAssetManager_open().
But this AAssetManager_xxxx() functions seem to be only available to access files in assets folder.
What I want to do is to access my .so file built in NDK. Still looking for answer.
If you want to use native files you need to use android NDK.
This link will help you if you use Eclipce: How do I import a native library (.so file) into Eclipse?
And this is for Android Studio: http://www.kylethielk.com/blog/include-native-so-library-in-apk-with-android-studio/
I hope it helps to resolve your problem.

Categories

Resources