渲染机器人EPUB文件机器人、文件、EPUB

2023-09-12 03:03:53 作者:情执

我有一个EPUB文件。我需要解压和解析EPUB文件,并使其在web视图。是否有一个教程一步一步的地方。

解决方案 访问这个网站并下载该网页中提到的两个JAR文件。 在导入这些库到你的Andr​​oid项目 在我实现了使用两个活动这一任务:   1)EpubReaderActivity - 本次活动将显示表的列表视图内容   2)ContentViewActivity - 这将显示所选章。

EpubReaderActivity.java

 公共类EpubReaderActivity扩展ListActivity
{

私人LayoutInflater充气;
私人列表< RowData> contentDetails;
公共静态最后弦乐BOOK_NAME =书籍/ wodehouse.epub;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    充气=(LayoutInflater)getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    contentDetails =新的ArrayList< RowData>();
    AssetManager assetManager = getAssets();
    尝试 {
        InputStream的epubInputStream = assetManager.open(BOOK_NAME);
        书书=(新EpubReader())readEpub(epubInputStream)。
        logContentsTable(book.getTableOfContents()getTocReferences(),0);
    }赶上(IOException异常E){
        Log.e(epublib,e.getMessage());
    }

    CustomAdapter适配器=新CustomAdapter(这一点,R.layout.list,
            R.id.title,contentDetails);
    setListAdapter(适配器);
    getListView()setTextFilterEnabled(真)。
}

私有类CustomAdapter扩展ArrayAdapter< RowData> {

    公共CustomAdapter(上下文的背景下,INT资源,
            INT textViewResourceId,名单,其中,RowData>对象){
        超(背景下,资源,textViewResourceId,对象);
    }

    私有类ViewHolder {
        私人视图行;
        私人TextView的拳王= NULL;

        公共ViewHolder(查看行){
            超();
            this.row =行;
        }

        公众的TextView的getTitle(){
            如果(空==拳王)
                拳王=(TextView中)row.findViewById(R.id.title);
            返回拳王;
        }
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        ViewHolder支架=无效;
        TextView的标题= NULL;
        RowData rowData =的getItem(位置);
        如果(空== convertView){
            convertView = inflater.inflate(R.layout.list,NULL);
            持有人=新ViewHolder(convertView);
            convertView.setTag(保持器);
        }
        支架=(ViewHolder)convertView.getTag();
        标题= holder.getTitle();
        title.setText(rowData.getTitle());
        返回convertView;
    }

}

私人无效logContentsTable(名单< TOCReference> tocReferences,诠释深度){
    如果(tocReferences == NULL){
        返回;
    }
    对于(TOCReference tocReference:tocReferences){
        StringBuilder的tocString =新的StringBuilder();
        的for(int i = 0; I<深度;我++){
            tocString.append(\ t的);
        }
        tocString.append(tocReference.getTitle());
        RowData行=新RowData();
        row.setTitle(tocString.toString());
        row.setResource(tocReference.getResource());
        contentDetails.add(行);
        logContentsTable(tocReference.getChildren(),深度+ 1);
    }
}

私有类RowData {
    私人字符串名称;
    私人资源的资源;

    公共RowData(){
        超();
    }

    公共字符串的getTitle(){
        返回称号;
    }

    公共资源的getResource(){
        返回的资源;
    }

    公共无效的setTitle(字符串名称){
        this.title =称号;
    }

    公共无效setResource(资源资源){
        this.resource =资源;
    }

}



@覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
    super.onListItemClick(L,V,位置ID);
    RowData rowData = contentDetails.get(位置);
    意向意图=新的意图(MicroEpubReaderActivity.this,ContentViewActivity.class);
    intent.putExtra(显示,新的String(rowData.getResource()的getData()));
    startActivity(意向);

}

}
 

ContentViewActivity.java

 公共类ContentViewActivity延伸活动{

的WebView web视图;

@覆盖
保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.content);

    web视图=(web视图)findViewById(R.id.webview);
    webView.getSettings()setJavaScriptEnabled(真)。

    字符串displayString = getIntent()getExtras()的getString(显示)。;
    如果(displayString!= NULL)
        webView.loadData(displayString,text / html的,UTF-8);
}
}
 

全部商品 菲菲书店 孔夫子旧书网

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.

解决方案

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

public class EpubReaderActivity extends ListActivity 
{

private LayoutInflater inflater;
private List<RowData> contentDetails;
public static final String BOOK_NAME = "books/wodehouse.epub";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    contentDetails = new ArrayList<RowData>();
    AssetManager assetManager = getAssets();
    try {
        InputStream epubInputStream = assetManager.open(BOOK_NAME);
        Book book = (new EpubReader()).readEpub(epubInputStream);
        logContentsTable(book.getTableOfContents().getTocReferences(), 0);
    } catch (IOException e) {
        Log.e("epublib", e.getMessage());
    }

    CustomAdapter adapter = new CustomAdapter(this, R.layout.list,
            R.id.title, contentDetails);
    setListAdapter(adapter);
    getListView().setTextFilterEnabled(true);
}

private class CustomAdapter extends ArrayAdapter<RowData>{

    public CustomAdapter(Context context, int resource,
            int textViewResourceId, List<RowData> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    private class ViewHolder{
        private View row;
        private TextView titleHolder = null;

        public ViewHolder(View row) {
            super();
            this.row = row;
        }

        public TextView getTitle() {
            if(null == titleHolder)
                titleHolder = (TextView) row.findViewById(R.id.title);
            return titleHolder;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        TextView title = null;
        RowData rowData = getItem(position);
        if(null == convertView){
            convertView = inflater.inflate(R.layout.list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        title = holder.getTitle();
        title.setText(rowData.getTitle());
        return convertView;
    }

}

private void logContentsTable(List<TOCReference> tocReferences, int depth) {
    if (tocReferences == null) {
        return;
    }
    for (TOCReference tocReference:tocReferences) {
        StringBuilder tocString = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            tocString.append("\t");
        }
        tocString.append(tocReference.getTitle());
        RowData row = new RowData();
        row.setTitle(tocString.toString());
        row.setResource(tocReference.getResource());
        contentDetails.add(row);
        logContentsTable(tocReference.getChildren(), depth + 1);
    }
}

private class RowData{
    private String title;
    private Resource resource;

    public RowData() {
        super();
    }

    public String getTitle() {
        return title;
    }

    public Resource getResource() {
        return resource;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }

}



@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    RowData rowData = contentDetails.get(position);
    Intent intent = new Intent(MicroEpubReaderActivity.this, ContentViewActivity.class);
    intent.putExtra("display", new String(rowData.getResource().getData()));
    startActivity(intent);

}

}

ContentViewActivity.java

public class ContentViewActivity extends Activity {

WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content);

    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);

    String displayString = getIntent().getExtras().getString("display");
    if(displayString != null)
        webView.loadData(displayString, "text/html", "utf-8");
}   
}

 
精彩推荐
图片推荐