2012-02-08 1 views
2

私はアンドロイドでePubファイルを作成しようとしています。以下は私のソースコードです。ePubを作成できません

しかし、私は、資産フォルダ内のcover.pngtest1.htmlを入れている

epubWriter.write(book, out);

NullPointerExceptionを取得しています。

*.otfファイル、OEBPS/*.opfMETA-INF/container.xmlmimetype*.cssでしょうか?

は、彼らはePubファイルを構築する義務はありますか?

私は、ファイルを作成することができるが、私は、デバイスからそのファイルを引っ張るとキャリバーでそれを表示しようとすると、それは次のようなエラーに

を与えることによって、開かないようそれが適切な形式ではありません、私は推測します

calibre, version 0.8.38 ERROR: Could not open ebook: File is not a zip file

私は絶対的な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は、私はあなたがたePubを作成するためのsiegmannのlibを使用している願っていますepubWriter.write(book, out);

+0

[アンドロイドでEPUBファイルを作成]の可能な複製(http://stackoverflow.com/questions/9174375/create-epub-file-in-android) –

+0

うん。申し訳ありません:。(... – GAMA

答えて

3

を示すことに注意してください。

から両方のjarファイルをダウンロードしてください2つのlibファイル(必須)

  1. epublibコア-latest.jar
  2. SLF4J - アンドロイド-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(); 
     } 


    } 
} 
+0

のthnxウル返事のため、私はあなたの提案のコードを試してみました、それは私を与える「口径、バージョン0.8.38 ERROR:開けませんでした電子書籍:このEPUB'の背骨に有効なエントリは...何が問題になるのでしょうか?あなたはEPUBブックを開く方法 – GAMA

+0

。あなたが任意のツールを使用していますか? – Prasad

+0

はい。私は 'Calibre'を使用しています。私は、ePubファイルをダウンロードし、Calibre''でそれを開いて、それが動作します。しかし、私ときこのコードで作成されたファイルを開こうとすると、動作しません。 – GAMA

関連する問題