在Android中从.epub文件获取图像

时间:2014-12-09 10:22:40

标签: android epublib

我正在使用siegmann.nl/epublib来读取epub文件。我的活动代码在这里提到。

public class EpubReaderActivity extends Activity 
{

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

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    titleListView = (ListView)findViewById(R.id.listView1);
    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);
    titleListView.setAdapter(adapter);

    titleListView.setTextFilterEnabled(true);

    titleListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub
               RowData rowData = contentDetails.get(position);
               Intent intent = new Intent(EpubReaderActivity.this, ContentViewActivity.class);
               try 
               {
                    intent.putExtra("display", new String(rowData.getResource().getData()));
               } 
               catch (IOException e)
               {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
                startActivity(intent);
        }
    });


    imgButtonBack = (ImageView)findViewById(R.id.imagBackButton);
    imgButtonBack.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(EpubReaderActivity.this, ListOfBook.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
            finish();
            overridePendingTransition(R.anim.slide_left,R.anim.slide_right);

        }
    });
}

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

}
}

以下是webView中的内容

public class ContentViewActivity extends Activity {

WebView webView;
ImageView imgButtonBack;

@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");
    imgButtonBack = (ImageView)findViewById(R.id.imagBackButton);
    imgButtonBack.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(ContentViewActivity.this, EpubReaderActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
            finish();
            overridePendingTransition(R.anim.slide_left,R.anim.slide_right);

        }
    });

   }   
}

当我从webview点击书页的图像时,我希望在下一个活动上获得一个图像。并且在LogCat上打印null。有人帮我怎么做。 感谢您的光临。

0 个答案:

没有答案