I am making a report in Unity3D with iTextSharp. When I start app from Unity it creates PDF and works perfect. But when I build App on Android device, I have a problem with creating font. Here is what I do:
BaseFont bf = BaseFont.CreateFont(System.IO.Path.Combine(Application.streamingAssetsPath, "ADOBEARABIC-BOLD_0.OTF"), BaseFont.IDENTITY_H, false);
As I have seen, the first parameter for font creation is the path to the font. My font is in /Assets/StreamingAssets/ folder.
When I try to load it with WWW class, it can find it, but when I give just the path to the creator it won't work.
Any idea what should I do? Or is there any other way to create a font that supports Arabic characters?
EDIT:
Ok, I have somehow managed to get to the font. First I copy font from Assets to the root of the APP:
IEnumerator CopyFiles()
{
string fromPath = Application.streamingAssetsPath + "/";
string toPath = Application.persistentDataPath + "/";
string filesNamesToCopy = "ADOBEARABIC-BOLD_0.OTF";
WWW www1 = new WWW(fromPath + filesNamesToCopy);
yield return www1;
File.WriteAllBytes(toPath + filesNamesToCopy , www1.bytes);
}
And then I make BaseFont:
BaseFont bf = BaseFont.CreateFont(System.IO.Path.Combine(Application.persistentDataPath, "ADOBEARABIC-BOLD_0.OTF"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Now I get new error when try to build on Android device:
ArgumentException: Encoding name 'windows-1252' not supported
Parameter: name
You have to put some DLL files to project folder. Which one depends what encoding you have to use.
http://answers.unity3d.com/questions/42955/codepage-1252-not-supported-works-in-editor-but-no.html
Related
I have been creating an app in Android Studio which uses OpenCV. I have my YOLOV4 models which I can use to detect humans. When I was using the Dnn class of org.opencv.dnn.Dnn, I need to provide the absolute path of the config and weights. Initially, I tried to put the files in my assets. But I was not able to get the path of the assets file.
I have tried putting the config and weights file in the external storage of my AVD. But it is showing -
Error: Parsing error (Failed to open NetParameter file: /storage/emulated/0/dnns/yolov4-tiny.cfg) in readNetFromDarknet
This is my code -
String yoloCfg = Environment.getExternalStorageDirectory().getAbsolutePath() + "/dnns/yolov4-tiny.cfg";
String yoloWeights = Environment.getExternalStorageDirectory().getAbsolutePath() + "/dnns/yolov4-tiny.weights";
yoloModel = Dnn.readNetFromDarknet(yoloCfg, yoloWeights);
Am I doing anything wrong or is it an issue?
For a robot simulation I use a csv file with data. I read data as follows:
string dbPath = ""; string realPath; // Android string oriPath = System.IO.Path.Combine(Application.streamingAssetsPath, "Data.csv");
// Android only use WWW to read file
WWW reader = new WWW(oriPath);
while (!reader.isDone) { }
realPath = Application.persistentDataPath + "/db";
System.IO.File.WriteAllBytes(realPath, reader.bytes);
dbPath = realPath;
using (StreamReader sr = new StreamReader(dbPath))
{...}
In the Unity Editor version the simulation works as expected but on Android the arm ofthe robot moves in a strange way as you can see in the videos. I compared the two db files (on the Windows and Android paths) and the content is identical. What could be the reason of the strange movemenz?
Editor https://streamable.com/7z4qob Android https://streamable.com/rv4nm2
Thank you!
To get StreamingAssets WWW you need to add prefix jar:file:///
To get Application.persistentDataPath you need to add prefix file:///
If files are identical then problem is not in getFile code, why did you provide it?
I am developing an app on android platform using QT. When I am using the FileDialog of the app to open a file on the mobile phone, I find the font size is too small for my finger to select a folder. How can I make the font size larger?
I tried the following code, but the font size doesn't change.
this->setStyleSheet("QTreeView {font: 40pt \"Arial\";}");
QString dir = QFileDialog::getExistingDirectory(NULL,"","/mnt/sdcard/test/");
My code is finally changed to this, but the font still doesn't change:
void MainWindow::on_pushButton_clicked()
{
QFileDialog *fdiag = new QFileDialog;
fdiag->setStyleSheet("QTreeView {font: 40pt \"Arial\";}"); // QFileDialog
// QString dir = QFileDialog::getExistingDirectory(this,"","/mnt/sdcard/test/",QFileDialog::DontUseNativeDialog);
QString dir = fdiag->getExistingDirectory(this,QString(),"/mnt/sdcard/test/",QFileDialog::DontUseNativeDialog);
model = new QFileSystemModel();
filesPath = dir;
model->setRootPath(dir);
I have some level definitions in xml format (with .txt extension) inside (without any subfolders) my project's rescources folder
I for more scalability, I have a plain text file naming all these level definition XMLs
I read that file using
TextAsset WorldList = (TextAsset)Resources.Load("WorldList");
And then I load the needed world:
TextAsset TA = (TextAsset)Resources.Load(/*"LevelDefs/" +*/ Worlds[worldToload]);
xps.parseXml(TA.text);
loadLevel(levelToLoad);
(you see that I have moved these rescources out of subfolder to reduce the chance of them not loading)
worldToload here is the index number of that world
program works fine on windows but nothing loads on my android test device.
I seem to have problems debugging on device so I only guess something went wrong in loading phase.
any suggestions?
From Unity Documentation:
Most assets in Unity are combined into the project when it is built.
However, it is sometimes useful to place files into the normal
filesystem on the target machine to make them accessible via a
pathname. An example of this is the deployment of a movie file on iOS
devices; the original movie file must be available from a location in
the filesystem to be played by the PlayMovie function.
Any files placed in a folder called StreamingAssets in a Unity project
will be copied verbatim to a particular folder on the target machine.
You can retrieve the folder using the Application.streamingAssetsPath
property. It’s always best to use Application.streamingAssetsPath to
get the location of the StreamingAssets folder, it will always point
to the correct location on the platform where the application is
running.
So below snippet should work:
// Put your file to "YOUR_UNITY_PROJ/Assets/StreamingAssets"
// example: "YOUR_UNITY_PROJ/Assets/StreamingAssets/Your.xml"
if (Application.platform == RuntimePlatform.Android)
{
// Android
string oriPath = System.IO.Path.Combine(Application.streamingAssetsPath, "Your.xml");
// Android only use WWW to read file
WWW reader = new WWW(oriPath);
while ( ! reader.isDone) {}
realPath = Application.persistentDataPath + "/Your"; // no extension ".xml"
var contents = System.IO.File.ReadAll(realPath);
}
else // e.g. iOS
{
dbPath = System.IO.Path.Combine(Application.streamingAssetsPath, "Your.xml");
}
Thanks to David I have resolved this problem.
for more precision I add some small details:
1-As David points out in his answer (and as stated in unity documentations), we should use StreamingAssets if we want to have the same folder structure. One should use Application.streamingAssetsPathto retrieve the path.
However, files are still in the apk package in android's case, so they should be accessed using unity android's www class.
a good practice here is to create a method to read the file (even mandatory if you are going to read more than one file)
here is one such method:
private IEnumerator LoadDataFromResources(string v)
{
WWW fileInfo = new WWW(v);
yield return fileInfo;
if (string.IsNullOrEmpty(fileInfo.error))
{
fileContents = fileInfo.text;
}
else
fileContents = fileInfo.error;
}
This is a coroutine, and to use it we need to call it using
yield return StartCoroutine(LoadDataFromResources(path + "/fileName.ext"));
Yet another remark: we can not write into this file or modify it as it's still inside the apk package.
So in my Eclipse android project I have a pdf file that I'd like to open, I looked up the standard address on the android developer's page and I came up with this pointer:
File file = new File("Android/data/com.alex.testing.app/res/raw/jazz.pdf");
where jazz.pdf is situated in res->raw in my eclipse project,
and com.alex.testing is my package name.
Still, when I try if(file.exists()) , the function returns false (on the emulator it goes to an else I've set up to display an error message)...
Sorry for the newbie question, but I'm really stuck with this :(.
put the file in assets folder and pick the file from there
Now use Context.getAssets().open("jazz.pdf") and pass the resulting InputStream into PDf parser library
Ok, to access resources from current application you can use something like,
Uri path = Uri.parse("android.resource://<you package>/raw/<your_file.pdf>");
OR
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" R.raw.<your_file.pdf>);
But I have a doubt if you are trying to use this pdf file in your application then its OK, but If you want to view this file using any third party application then I think you can't do it.
Because external application can't access application's package resources file.
So better way it to put this file in /asset directory then copy it to any public access area then view that file from that path.
//if your are stored in SDcard your location would be
"data/data/com.alex.testing/res/raw/jazz.pdf"
//you read resources from raw folder thru the below code
InputStream inputStream = getResources().openRawResource(R.raw.jazz);
byte[] reader = new byte[inputStream.available()];