GAMA
GAMA

Reputation: 5996

Create EPub file in android

I'm trying to create an ePub file in android.

Below is my source code.

But I'm getting fileNotFoundException at

epubWriter.write(book, new FileOutputStream("test1_book1.epub"));

I've put the cover.png and test1.html in assets folder.

Is corresponding css file is compulsory to build an ePub file?

I'm absolute beginner to ePub development so any help/suggestion would be appreciated.

CreateEPub.java

public class CreateEPub extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AssetManager assetManager = getAssets();

        try {
            Book book = new Book();
            book.getMetadata().addTitle("Epub test book 1");
            book.getMetadata().addAuthor(new Author("Joe", "Tester"));
            InputStream is = assetManager.open("cover.png");
            book.getMetadata().setCoverImage(new Resource(is, "cover.png"));
            // Add Chapter 1
            InputStream is1 = assetManager.open("test1.html");
            book.addSection("Introduction", new Resource(is1, "chapter1.html"));

            EpubWriter epubWriter = new EpubWriter();
            epubWriter.write(book, new FileOutputStream("test1_book1.epub"));
            Log.v("ePub", "Created");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

EDIT

From various suggestions, Now I'm able to create a file but that is not in proper format I guess as when I pull that file from device and try to view it, it doesn't open giving error that It is not a zip file.

LogCat

java.lang.NullPointerException
at org.kxml2.io.KXmlSerializer.attribute(KXmlSerializer.java:473)
at nl.siegmann.epublib.epub.PackageDocumentMetadataWriter.writeMetaData(PackageDocumentMetadataWriter.java:93)
at nl.siegmann.epublib.epub.PackageDocumentWriter.write(PackageDocumentWriter.java:45)
at nl.siegmann.epublib.epub.EpubWriter.writePackageDocument(EpubWriter.java:112)
0at nl.siegmann.epublib.epub.EpubWriter.write(EpubWriter.java:53)
at com.createepub.CreateEPub.onCreate(CreateEPub.java:93)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
0at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

NOTE : CreateEPub.java:93 referes to epubWriter.write(book, out);

Upvotes: 0

Views: 2299

Answers (2)

eks
eks

Reputation: 511

The problem is yet not the epubwriter but the FileOutputStream. The String-parameter it needs is not the name of the file, but the path of the file. check the documentation here. http://developer.android.com/reference/java/io/FileOutputStream.html

i would recommend to use the file parameter version of FileOutputStream. get a file from your context with getFilesDir() and add your file to this directory, etc. Be careful about the permissions. Your application needs the proper permissions, if you are writing your file on the external storage.

edit: i can't comment, i hope you read this. createepub is an activity and every activity is a context. activity "extends" context. http://developer.android.com/reference/android/app/Activity.html

Upvotes: 1

devconsole
devconsole

Reputation: 7925

You can write files only to Internal Storage (device memory) or External Storage (the SD card), see data-storage. Simply opening a FileOutputStream with an arbitrary file name won't work since that file system is read-only.

Upvotes: 1

Related Questions