无法创建 ePub

发布于 2025-01-03 02:46:32 字数 3102 浏览 2 评论 0原文

我正在尝试在 android 中创建 ePub 文件。下面是我的源代码。

处收到 NullPointerException

但我在epubWriter.write(book, out);

我已经将 cover.pngtest1 .html 在资产文件夹中。

*.cssmimetypeMETA-INF/container.xmlOEBPS/*.opf*.otf 文件?

他们是否必须构建 ePub 文件?

我可以创建一个文件,但格式不正确,我猜当我从设备中提取该文件并尝试在 Calibre 中查看它时,它不会打开,并给出以下错误

<代码>口径,版本0.8.38 错误:无法打开电子书:文件不是 zip 文件

我是 ePub 开发的绝对初学者,因此任何帮助/建议将不胜感激。

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();
        }
    }
}

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)
at nl.siegmann.epublib.epub.EpubWriter.write(EpubWriter.java:53)
at com.createepub.CreateEPub.onCreate(CreateEPub.java:91)
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)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

注意 CreateEPub 第 91 行引用 epubWriter.write(book, out);

I'm trying to create an ePub file in android. Below is my source code.

But I'm getting NullPointerException at

epubWriter.write(book, out);

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

What about *.css,mimetype,META-INF/container.xml,OEBPS/*.opf,*.otf files?

Are they compulsory to build an ePub file?

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 in Calibre, it doesn't open by giving following error

calibre, version 0.8.38
ERROR: Could not open ebook: File is not a zip 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();
        }
    }
}

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)
at nl.siegmann.epublib.epub.EpubWriter.write(EpubWriter.java:53)
at com.createepub.CreateEPub.onCreate(CreateEPub.java:91)
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)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

Note that CreateEPub Line 91 refers to epubWriter.write(book, out);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2025-01-10 02:46:32

我希望您使用 siegmann lib 来创建 ePub。

需要包含 2 个 lib 文件(强制)

  1. epublib-core-latest.jar
  2. slf4j-android-1.6.1-RC1.jar

http://www.siegmann.nl/epublib/android

示例代码

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.epub.EpubWriter;
import android.app.Activity;
import android.os.Bundle;



public class EpubAppActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Book b =  new Book();
        b.getMetadata().addTitle("test epub book");
        b.getMetadata().addAuthor(new Author("author name"));

       EpubWriter w = new EpubWriter();

        FileOutputStream fos;
        try {

            File file = new File(getApplicationContext().getExternalFilesDir(null), "test.epub");
            if(!file.exists()){
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            w.write(b, fos);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}

I hope you are using siegmann lib for creating ePub.

Need to include 2 lib files(mandatory)

  1. epublib-core-latest.jar
  2. slf4j-android-1.6.1-RC1.jar

Download both jar from http://www.siegmann.nl/epublib/android

Sample Code

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.epub.EpubWriter;
import android.app.Activity;
import android.os.Bundle;



public class EpubAppActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Book b =  new Book();
        b.getMetadata().addTitle("test epub book");
        b.getMetadata().addAuthor(new Author("author name"));

       EpubWriter w = new EpubWriter();

        FileOutputStream fos;
        try {

            File file = new File(getApplicationContext().getExternalFilesDir(null), "test.epub");
            if(!file.exists()){
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            w.write(b, fos);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文