Render Epub Files In Android
I have a epub file. I need to unzip and parse the epub file and render it in Webview. Is there a step by step tutorial somewhere.
Solution 1:
- Visit this site and download the two jar files mentioned in that page.
- Import those libraries to your android project
- I implemented this task using two activities : 1.) EpubReaderActivity - this activity will display a list view of Table of Contents 2.) ContentViewActivity - this will display the selected chapter.
EpubReaderActivity.java
publicclassEpubReaderActivityextendsListActivity
{
private LayoutInflater inflater;
private List<RowData> contentDetails;
publicstaticfinalStringBOOK_NAME="books/wodehouse.epub";
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
contentDetails = newArrayList<RowData>();
AssetManagerassetManager= getAssets();
try {
InputStreamepubInputStream= assetManager.open(BOOK_NAME);
Bookbook= (newEpubReader()).readEpub(epubInputStream);
logContentsTable(book.getTableOfContents().getTocReferences(), 0);
} catch (IOException e) {
Log.e("epublib", e.getMessage());
}
CustomAdapteradapter=newCustomAdapter(this, R.layout.list,
R.id.title, contentDetails);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
privateclassCustomAdapterextendsArrayAdapter<RowData>{
publicCustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
privateclassViewHolder{
private View row;
privateTextViewtitleHolder=null;
publicViewHolder(View row) {
super();
this.row = row;
}
public TextView getTitle() {
if(null == titleHolder)
titleHolder = (TextView) row.findViewById(R.id.title);
return titleHolder;
}
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
ViewHolderholder=null;
TextViewtitle=null;
RowDatarowData= getItem(position);
if(null == convertView){
convertView = inflater.inflate(R.layout.list, null);
holder = newViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.getTitle();
title.setText(rowData.getTitle());
return convertView;
}
}
privatevoidlogContentsTable(List<TOCReference> tocReferences, int depth) {
if (tocReferences == null) {
return;
}
for (TOCReference tocReference:tocReferences) {
StringBuildertocString=newStringBuilder();
for (inti=0; i < depth; i++) {
tocString.append("\t");
}
tocString.append(tocReference.getTitle());
RowDatarow=newRowData();
row.setTitle(tocString.toString());
row.setResource(tocReference.getResource());
contentDetails.add(row);
logContentsTable(tocReference.getChildren(), depth + 1);
}
}
privateclassRowData{
private String title;
private Resource resource;
publicRowData() {
super();
}
public String getTitle() {
return title;
}
public Resource getResource() {
return resource;
}
publicvoidsetTitle(String title) {
this.title = title;
}
publicvoidsetResource(Resource resource) {
this.resource = resource;
}
}
@OverrideprotectedvoidonListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
RowDatarowData= contentDetails.get(position);
Intentintent=newIntent(MicroEpubReaderActivity.this, ContentViewActivity.class);
intent.putExtra("display", newString(rowData.getResource().getData()));
startActivity(intent);
}
}
ContentViewActivity.java
publicclassContentViewActivityextendsActivity {
WebView webView;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
setContentView(R.layout.content);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
StringdisplayString= getIntent().getExtras().getString("display");
if(displayString != null)
webView.loadData(displayString, "text/html", "utf-8");
}
}
Solution 2:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".ListActivity" ><ListViewandroid:id="@android:id/list"android:layout_width="match_parent"android:layout_height="wrap_content" ></ListView><TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/title"android:layout_width="fill_parent"android:layout_height="wrap_content"android:padding="10dip"android:textSize="16dip"android:textStyle="bold" ></TextView>
main.xml Use this with pkamalaruban's answer
Solution 3:
If you want to use stage web view for rendering epub simply pass the epub directly....The android native web kit browser supports almost all formats for rendering.
var stageWebView:StageWebView = new StageWebView;
stageWebView.stage = stage;
stageWebView.viewPort = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight-100);
stageWebView.loadURL("http://localhost/eboard.pdf"); // use any path
You dont require any zip/unzip or parsing....but in case you do not go for web view then you might need that...
Post a Comment for "Render Epub Files In Android"