安卓:EPUB文件未显示在模拟器图像/ Android设备 [英] Android: Epub file not showing images in emulator/android device
问题描述
我使用 http://www.siegmann.nl/epublib 阅读EPUB文件。我的code为如下所述。
{尝试
本书= epubReader.readEpub(新的FileInputStream(/ SD卡/ EpubTesting.epub)); 资源资源;
脊柱内容= book.getSpine(); 清单< SpineReference> spinelist = contents.getSpineReferences();
StringBuilder的字符串=新的StringBuilder();
串线= NULL;
诠释计数= spinelist.size();
的for(int i = 0; I<计数;我++){
RES = contents.getResource(I)
尝试{
InputStream为= res.getInputStream();
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是)); 尝试{
而((行= reader.readLine())!= NULL){
。linez =(string.append(行+\\ n))的toString();
} }赶上(IOException异常五){e.printStackTrace();}
}赶上(IOException异常五){
e.printStackTrace();
}
}
的System.out.println(linez);
s1.loadDataWithBaseURL(/ SD卡/,linez,text / html的,UTF-8,NULL); }赶上(FileNotFoundException异常五){ Toast.makeText(mContext,找不到文件,Toast.LENGTH_SHORT).show();
}赶上(IOException异常五){
Toast.makeText(mContext,IO异常,Toast.LENGTH_SHORT).show();
}
也试过
s1.loadDataWithBaseURL(,linez,text / html的,UTF-8,NULL);
s1.loadDataWithBaseURL(文件:// MNT / SD卡/,linez,text / html的,UTF-8,NULL);
但结果是sifar。请告诉我,我必须做的,以显示文件中所包含的图像。我已经经历了常见问题解答说让 android.webkit.WebView
的子类,重载的使用loadURL(字符串)
方法它加载从书而不是互联网上的图像的方式。但直到我没有在那里提取的文件我怎么能找到路径。请告诉我。我很迷茫。先谢谢了。
公共类EpubBookContentActivity延伸活动{私有静态最后弦乐TAG =EpubBookContentActivity;
的WebView的WebView;书书;INT位置= 0;串线;
INT I = 0;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.content); 网页流量=(的WebView)findViewById(R.id.webView);
webview.getSettings()setJavaScriptEnabled(真)。 AssetManager assetManager = getAssets();
的String []文件; 尝试{ 文件= assetManager.list(书);
清单<串GT;清单= Arrays.asList(文件); 如果(!this.makeDirectory(书)){
调试(faild使书目录);
} copyBookToDevice(list.get(位置)); 字符串基本路径= Environment.getExternalStorageDirectory()+/书籍/; InputStream的epubInputStream = assetManager.open(书籍/+ list.get(位置)); 书=(新EpubReader())readEpub(epubInputStream)。 DownloadResource(基本路径); 串linez =;
脊柱的脊椎= book.getSpine();
清单< SpineReference> spineList = spine.getSpineReferences();
诠释计数= spineList.size(); StringBuilder的字符串=新的StringBuilder();
的for(int i = 0;计数I标记;我++){ 资源解析度= spine.getResource(I) 尝试{
InputStream为= res.getInputStream();
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是));
尝试{
而((行= reader.readLine())!= NULL){
。linez = string.append(行+\\ n)的toString();
} }赶上(IOException异常五){e.printStackTrace();}
}赶上(IOException异常五){
e.printStackTrace();
} } linez = linez.replace(../,);//文件文件=新的文件(Environment.getExternalStorageDirectory()的test.html);
// file.createNewFile();
// FileOutputStream中的FileOutputStream =新的FileOutputStream(文件);
// fileOutputStream.write(linez.getBytes());
// fileOutputStream.close();
webview.loadDataWithBaseURL(文件://+ Environment.getExternalStorageDirectory()+/书籍/,linez,text / html的,UTF-8,NULL); }赶上(IOException异常五){
Log.e(epublib异常,e.getMessage());
}
}公共布尔makeDirectory(字符串目录名){
布尔水库; 字符串文件路径=新的String(Environment.getExternalStorageDirectory()+/+目录名); 调试(文件路径);
档案文件=新的文件(文件路径);
如果(!file.exists()){
RES = file.mkdirs();
}其他{
RES = FALSE;
}
返回水库;
}公共无效调试(弦乐味精){
//如果(Setting.isDebug()){
Log.d(EPUB,味精);
//}
}公共无效copyBookToDevice(字符串文件名){
的System.out.println(复制簿中donwload在手机文件夹);
尝试
{
InputStream的localInputStream = getAssets()开(书籍/+文件名)。
字符串路径= Environment.getExternalStorageDirectory()+/书籍/+文件名;
FileOutputStream中localFileOutputStream =新的FileOutputStream(路径); 字节[] = arrayOfByte新的字节[1024];
INT抵消;
而((偏移= localInputStream.read(arrayOfByte))大于0)
{
localFileOutputStream.write(arrayOfByte,0,偏移);
}
localFileOutputStream.close();
localInputStream.close();
Log.d(TAG,文件名+复制到手机); }
赶上(IOException异常localIOException)
{
localIOException.printStackTrace();
Log.d(TAG,无法复制);
返回;
}
}私人无效DownloadResource(字符串目录){
尝试{ 资源RST = book.getResources();
收集和LT;资源> clrst = rst.getAll();
迭代器<资源与GT; ITR = clrst.iterator(); 而(itr.hasNext()){
资源RS = itr.next(); 如果((rs.getMediaType()== MediatypeService.JPG)
|| (rs.getMediaType()== MediatypeService.PNG)
|| (rs.getMediaType()== MediatypeService.GIF)){ Log.d(TAG,rs.getHref()); 文件oppath1 =新的文件(目录,rs.getHref()代替(OEBPS /,)); oppath1.getParentFile()mkdirs()。
oppath1.createNewFile(); 的System.out.println(路径:+ oppath1.getParentFile()getAbsolutePath());
FileOutputStream中fos1 =新的FileOutputStream(oppath1);
fos1.write(rs.getData());
fos1.close(); }否则如果(rs.getMediaType()== MediatypeService.CSS){ 文件oppath =新的文件(目录,rs.getHref()); oppath.getParentFile()mkdirs()。
oppath.createNewFile(); FOS的FileOutputStream =新的FileOutputStream(oppath);
fos.write(rs.getData());
fos.close(); } }
}赶上(例外五){ }
}
}
I am using http://www.siegmann.nl/epublib to read epub file. My code is mentioned below.
try {
book = epubReader.readEpub(new FileInputStream("/sdcard/EpubTesting.epub"));
Resource res;
Spine contents = book.getSpine();
List<SpineReference> spinelist = contents.getSpineReferences();
StringBuilder string = new StringBuilder();
String line = null;
int count = spinelist.size();
for (int i=0;i<count;i++){
res = contents.getResource(i);
try {
InputStream is = res.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((line = reader.readLine()) != null) {
linez = (string.append(line+"\n")).toString();
}
} catch (IOException e) {e.printStackTrace();}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(linez);
s1.loadDataWithBaseURL("/sdcard/",linez, "text/html", "UTF-8",null);
}catch (FileNotFoundException e) {
Toast.makeText(mContext, "File not found.", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(mContext, "IO Exception.", Toast.LENGTH_SHORT).show();
}
Also tried
s1.loadDataWithBaseURL("",linez, "text/html", "UTF-8",null);
s1.loadDataWithBaseURL("file://mnt/sdcard/",linez, "text/html", "UTF-8",null);
But result is sifar. Please tell me what I have to do to show the contained images in file. I have gone through FAQ says Make a subclass of android.webkit.WebView
that overloads the loadUrl(String)
method in such a way that it loads the image from the Book instead of the internet. But till I don't where they extract the file how can I locate the path. Please tell me. I am very confused. Thanks in advance.
public class EpubBookContentActivity extends Activity{
private static final String TAG = "EpubBookContentActivity";
WebView webview;
Book book;
int position = 0;
String line;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
AssetManager assetManager = getAssets();
String[] files;
try {
files = assetManager.list("books");
List<String> list =Arrays.asList(files);
if (!this.makeDirectory("books")) {
debug("faild to make books directory");
}
copyBookToDevice(list.get(position));
String basePath = Environment.getExternalStorageDirectory() + "/books/";
InputStream epubInputStream = assetManager.open("books/"+list.get(position));
book = (new EpubReader()).readEpub(epubInputStream);
DownloadResource(basePath);
String linez = "";
Spine spine = book.getSpine();
List<SpineReference> spineList = spine.getSpineReferences() ;
int count = spineList.size();
StringBuilder string = new StringBuilder();
for (int i = 0; count > i; i++) {
Resource res = spine.getResource(i);
try {
InputStream is = res.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((line = reader.readLine()) != null) {
linez = string.append(line + "\n").toString();
}
} catch (IOException e) {e.printStackTrace();}
} catch (IOException e) {
e.printStackTrace();
}
}
linez = linez.replace("../", "");
// File file = new File(Environment.getExternalStorageDirectory(),"test.html");
// file.createNewFile();
// FileOutputStream fileOutputStream = new FileOutputStream(file);
// fileOutputStream.write(linez.getBytes());
// fileOutputStream.close();
webview.loadDataWithBaseURL("file://"+Environment.getExternalStorageDirectory()+"/books/", linez, "text/html", "utf-8", null);
} catch (IOException e) {
Log.e("epublib exception", e.getMessage());
}
}
public boolean makeDirectory(String dirName) {
boolean res;
String filePath = new String(Environment.getExternalStorageDirectory()+"/"+dirName);
debug(filePath);
File file = new File(filePath);
if (!file.exists()) {
res = file.mkdirs();
}else {
res = false;
}
return res;
}
public void debug(String msg) {
// if (Setting.isDebug()) {
Log.d("EPub", msg);
// }
}
public void copyBookToDevice(String fileName) {
System.out.println("Copy Book to donwload folder in phone");
try
{
InputStream localInputStream = getAssets().open("books/"+fileName);
String path = Environment.getExternalStorageDirectory() + "/books/"+fileName;
FileOutputStream localFileOutputStream = new FileOutputStream(path);
byte[] arrayOfByte = new byte[1024];
int offset;
while ((offset = localInputStream.read(arrayOfByte))>0)
{
localFileOutputStream.write(arrayOfByte, 0, offset);
}
localFileOutputStream.close();
localInputStream.close();
Log.d(TAG, fileName+" copied to phone");
}
catch (IOException localIOException)
{
localIOException.printStackTrace();
Log.d(TAG, "failed to copy");
return;
}
}
private void DownloadResource(String directory) {
try {
Resources rst = book.getResources();
Collection<Resource> clrst = rst.getAll();
Iterator<Resource> itr = clrst.iterator();
while (itr.hasNext()) {
Resource rs = itr.next();
if ((rs.getMediaType() == MediatypeService.JPG)
|| (rs.getMediaType() == MediatypeService.PNG)
|| (rs.getMediaType() == MediatypeService.GIF)) {
Log.d(TAG, rs.getHref());
File oppath1 = new File(directory, rs.getHref().replace("OEBPS/", ""));
oppath1.getParentFile().mkdirs();
oppath1.createNewFile();
System.out.println("Path : "+oppath1.getParentFile().getAbsolutePath());
FileOutputStream fos1 = new FileOutputStream(oppath1);
fos1.write(rs.getData());
fos1.close();
} else if (rs.getMediaType() == MediatypeService.CSS) {
File oppath = new File(directory, rs.getHref());
oppath.getParentFile().mkdirs();
oppath.createNewFile();
FileOutputStream fos = new FileOutputStream(oppath);
fos.write(rs.getData());
fos.close();
}
}
} catch (Exception e) {
}
}
}
这篇关于安卓:EPUB文件未显示在模拟器图像/ Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!