将资源加载到位图,该资源取自 nl.siegmann.epublib
使用 nl.siegmann.epublib 我从一本书中获取了资源,并尝试将其实际显示在屏幕上。
api 文档可以在此处找到,
我使用的代码是: Book.getCoverImage() 和 Book.getCoverPage()。
实际代码看起来更像这样:
Book book = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = (ImageView)findViewById(R.id.image_test);
try{
Resource res = bookLoaded().getCoverImage();
Bitmap bm = BitmapFactory.decodeStream(res.getInputStream());
iv.setImageBitmap(bm);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public Book bookLoaded(){
AssetManager am = getAssets();
try{
InputStream is = am.open("Ada Madison - [Sophie Knowles Mystery 01] - The Square Root of Murder (epub).epub");
book = (new EpubReader()).readEpub(is);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return book;
}
现在我遇到的问题是使用 .getCoverImage() 时 res 始终为 null。当我尝试 .getCoverPage(); 时出现异常错误
我的问题是“使用 BitmapFactory 是执行此操作的正确方法吗?我做错了什么吗?或者我错过了什么?”。
继续我迄今为止的研究: 您可以获得 .epub 路径。使用 book.GetHref()。然而,问题更多的是 .epub 文件(类似于 zip 文件)如何获取其中的数据。然后现在我必须寻找一个提取器,然后我才能找到图像文件。
try{
byte[] newData = bookLoaded().getCoverImage().getData();
Bitmap bmp =BitmapFactory.decodeByteArray(newData, 0, newData.length);
image_view.setImageBitmap(bmp);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
不知何故 bookLoaded().getCoverImage().getData();引起异常
Using nl.siegmann.epublib I have taken a resource from a book which I try to actually display on the screen.
The api docs can be found here
The code I used are:
Book.getCoverImage() and Book.getCoverPage().
Actual Code looks more like this:
Book book = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = (ImageView)findViewById(R.id.image_test);
try{
Resource res = bookLoaded().getCoverImage();
Bitmap bm = BitmapFactory.decodeStream(res.getInputStream());
iv.setImageBitmap(bm);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public Book bookLoaded(){
AssetManager am = getAssets();
try{
InputStream is = am.open("Ada Madison - [Sophie Knowles Mystery 01] - The Square Root of Murder (epub).epub");
book = (new EpubReader()).readEpub(is);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return book;
}
Now the problem I occurred was res was always null using .getCoverImage(). and I had an exception error when I tried .getCoverPage();
My question is "Is using BitmapFactory the proper way to do this?, am I doing something wrong?, or am I missing something?".
To continue of what I researched so far:
You can get the .epub path. using book.GetHref(). However, the question is more of how can an .epub file(Similar to a zip file) get data inside of it. Then now I have to look for an extractor, and then I can find the image file.
try{
byte[] newData = bookLoaded().getCoverImage().getData();
Bitmap bmp =BitmapFactory.decodeByteArray(newData, 0, newData.length);
image_view.setImageBitmap(bmp);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Somehow bookLoaded().getCoverImage().getData(); caused an exception
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是有史以来最蹩脚的错误。
这段代码有效。然而,最大的问题是 .xml,其中 textview 与图像重叠,然后不显示图像。该代码可能会也可能不会工作,因为库将创建它或不创建它的位置。如果没有,您可能必须在 .getResources() 上查找它。
This was the lamest bug ever.
This code works. However the biggest issue is on .xml where textview overlapped the image and then did not show the image. This code may or may not work as the library will either create a location of it or not. If it does not you may have to look for it on .getResources().