Create a Word Document & Save to DOCX DOC PDF or TXT File or Stream on Android 9 | Write Permissions

Hi all!

I’m trying Aspose.Words for Android with the restricted evaluation version (free download). Unfortunately, I have problems already when trying the simplest examples: whenever saving a document (be it read from an existing file or a newly created one), a file is created with the name I specify, but this file is empty (0 bytes). The problem occurs with .docx, .doc, and .txt files; I haven’t tried other formats yet.

Here’s the code of one example which doesn’t work:

Document doc = new Document();
DocumentBuilder docb = new DocumentBuilder(doc);
docb.writeln("Test");

FileOutputStream stream = new FileOutputStream("/sdcard/Documents/FormatTest.doc");

doc.save(stream, SaveFormat.DOC);
stream.close();

No exception is thrown, and I can’t find anything helpful in the logs.

It doesn’t seem to be a problem related to permissions or other general Android storing issues: when I write a bunch of bytes directly to the FileOutputStream with stream.write(…), the bytes are written correctly.

I’m using Android 9 and Aspose.Words v. 20.9.0.

Thank you very much in advance for any help!

Cheers,

Chris

@christopher.kopel,

After an initial test with the licensed latest (20.9) version of Aspose.Words for Android via Java, we were unable to reproduce this issue on our end. We used the following simple code to test the scenario over Android 10 on our end:

License lic = new License();
lic.setLicense(pathto_License);

Document doc = new Document();
DocumentBuilder docb = new DocumentBuilder(doc);
docb.writeln("Test");

doc.save(outputPathof_DOCX);
doc.save(outputPath_PDF);

Can you please provide your Android Studio project to be able to reproduce the exact problem on our end?

Hi,

Thank you for your quick reply!

Here’s my AndroidStudio project. I removed the Aspose.Words jar file from the zip archive in order to reduce the file size; in my project, I put the library in the subdirectory app/libs and named it aspose.jar.

Thank you for your help!

Cheers,

Chris

Aspose-Words-Test-Project.zip (458.0 KB)

@christopher.kopel,

We are working on your query and will get back to you soon.

@christopher.kopel,

I am afraid, we are still unable to reproduce this problem on our end. Please see the following code that we used on our end for testing with 20.9 version of Aspose.Words for Android via Java API.

Hello!

Thank you for your reply and the sample code. I created a project in Android Studio with exactly the same package name and main activity code. Unfortunately, the problem still occurs.

The only thing I changed is that I had to comment out the line “lic.setLicence(…);” as I do not have any licence file yet. Could this be the reason?

I noticed that you import a legacy Android support library (android.support.v7.app.AppCompatActivity). Which Android version do you use as target version?

Best regards,

Chris

@christopher.kopel,

Your application does not seem to have write permissions. Instead of writing to File System of Android device, you can try to save Aspose.Words’ Document object to memory stream and then reload the DOC into another Document object. Then print content of final Document object to determine if Aspose.Words is working as expected or not.

try {
    String licString = Environment.getExternalStorageDirectory().getAbsolutePath() + "/lic/Aspose.Total.Product.Family.lic";
    String outputPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JAND-20.9.doc";

    License lic = new License();
    lic.setLicense(licString);

    Document doc = new Document();
    DocumentBuilder docb = new DocumentBuilder(doc);
    docb.writeln("Test");

    // FileOutputStream stream = new FileOutputStream(outputPath);
    // doc.save(stream, SaveFormat.DOC);

    // Save Aspose.Words' Document to DOC stream
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    doc.save(baos, SaveFormat.DOC);
    // Reload DOC stream into new Instance
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    Document finalDoc = new Document(bais);
    // Write content to TextView
    TextView txtView = (TextView) findViewById(R.id.textView);
    txtView.setText(finalDoc.toString(SaveFormat.TEXT).trim());
} catch (Exception e) {

}   

If you want to test ‘Aspose.Words for Android via Java’ without the evaluation version limitations, you can also request a 30-day Temporary License. Please refer to How to get a Temporary License?