i have an very strange behavoir in my android app.
The user have three ways to write a file: Excel, txt and pdf.
In my string it is possible that i have character just like this: "ä", "ß".
For the excel and txt File i use the following code to write:
FileWriter fWriter;
try
{
String extr = sdCard.toString();
File mFolder = new File(extr + "/" + FileUtil.TEST_DICTONARY);
if (!mFolder.exists())
{
mFolder.mkdir();
}
File mFile = new File(mFolder.getAbsolutePath(), FileUtil.TEXT_FILENAME);
mFile.delete();
String appDataToWrite = createAppDataToWriteToFile(appData, resources);
fWriter = new FileWriter(mFile, false);
fWriter.write(appDataToWrite);
fWriter.flush();
fWriter.close();
}
catch (NotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
This works perfect, the encoding is i excepted. If i use the same for the pdf file then it's not the right encoding. After searching in SO i use the following:
try
{
String extr = sdCard.toString();
File mFolder = new File(extr + "/" + FileUtil.TEST_DICTONARY);
if (!mFolder.exists())
{
mFolder.mkdir();
}
File mFile = new File(mFolder.getAbsolutePath(), FileUtil.PDF_FILENAME);
mFile.delete();
String appDataToWrite = createAppDataToWriteToPdf(appData, resources);
FileOutputStream fos = new FileOutputStream(mFile, false);
OutputStreamWriter char_output = new OutputStreamWriter(fos, Charset.forName("UTF-8").newEncoder());
char_output.write(appDataToWrite);
char_output.flush();
char_output.close();
}
catch (NotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
I have no idea why this getting wrong. For your information, I'm using the following library for generating the pdf http://coderesearchlabs.com/androidpdfwriter/
I have debug the code and a can see that the OutputStreamWriter getting the right input.
Any suggestions?
Thanks, Manu