I need read .txt file on android using qt program. If build for windows, that file should be in build folder, but what about android? How to include it in apk?
You can include the text file into your APK's assets, and then refer to it like so:
QFile file( "assets:/qml/foo/main.qml" );
This question has another URI for accessing assets: How to add qt resources to android APK file?
Or you can package the QFile in your application as a QRC (http://qt-project.org/doc/qt-5.0/qtcore/resources.html) and access it like so:
QFile file( "qrc:/path/to/file.abc" );
Both of these options are read only.
Related
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);
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/
I would like to know how to access the assets directory from android (file path)
I tried something like :
Uri.parse("file:///android_asset/Resources/sound/" + sound);
But it doesn't work !
I know that I could do that easily with Titanium BUT I need the URI file from the JAVA module ...
Any idea ?
No Uri or File class.
You have to use the assets manager and then open an inputstream for your file and read from the stream.
This question already has answers here:
How To Get File In Assets From Android NDK
(2 answers)
Opening a File from assets folder in android
(6 answers)
Closed 8 years ago.
I wrote a small test application that uses SDL2 on Android via the NDK. It compiles and runs. However, as soon as I try to open files I get FileNotFound Exceptions. I added an assets Directory in my eclipse workdir and put files inside it. When I Export the projekt to an .apk, I can open the file in 7z and see that there is an assets Directory with those files in it. However, when the code gets executed and I try to open assets/somefile.txt, I get a FileNotFound Exception.
Google so far told me that I could put an sdcard in my device with those files on it and access it through /sdcard/ . I'ld like to include those files in my apk though.
How do I Access the files I put in assets ? Or where should I put those files and how should I adjust my path it can Access them at runtime?
Place this code before anything else in your main activity ->
if (Environment.getExternalStorageState (). equals (Environment.MEDIA_MOUNTED)) {
File directory = new File (Environment.getExternalStorageDirectory () + + File.separator "YourFolderName");
directory.mkdirs ();
}
Give this permission in the Manifest xml ->
<uses-permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" />
I need to have some files in android assets folder, how can I add them using QtCreator/QMake?
Assuming you have the following structure in your source directory:
foo.pro
extra_data/file1
extra_data/file2
…
Adding the following to foo.pro should deploy the extra_data folder to assets://extra_data (exact path might differ, cannot verify right now) in the APK:
folder_01.source = extra_data
folder_01.target = extra_data
DEPLOYMENTFOLDERS += folder_01
If you are developing the application, then simply copy/paste the files in assets folder.
But if your application is already built and available as .apk file then you cannot modify any of its content.
Copying files to /5.2.0/android_armv7/src/android/java/assets did the trick