Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 正在将资源加载到位图,从nl.siegmann.epublib获取哪个资源_Android_Imageview_Inputstream - Fatal编程技术网

Android 正在将资源加载到位图,从nl.siegmann.epublib获取哪个资源

Android 正在将资源加载到位图,从nl.siegmann.epublib获取哪个资源,android,imageview,inputstream,Android,Imageview,Inputstream,使用nl.siegmann.epublib,我从一本书中获取了一个资源,并试图将其实际显示在屏幕上 可以找到api文档 我使用的代码是: Book.getCoverImage()和Book.getCoverPage() 实际代码看起来更像这样: Book book = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState

使用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。我在尝试时遇到了一个异常错误

我的问题是“使用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();导致异常

这是有史以来最糟糕的错误

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();
         }
这个代码有效。然而,最大的问题是.xml,其中textview与图像重叠,然后没有显示图像。此代码可能有效,也可能无效,因为库将创建它的位置或不创建它。如果没有,您可能必须在.getResources()上查找它

Bitmap bitmap = null;
try{
    Book book = (new EpubReader()).readEpub(new FileInputStream("/sdcard/example.epub"));
    Resource resource = book.getResources().getById("cover");
    if (resource != null){
        byte[] coverImage = resource.getData();
        bitmap = BitmapFactory.decodeByteArray(coverImage, 0, coverImage.length);                       
    } 
}catch(Exception ex){
    Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT);
}

ImageView imageView = (ImageView) findViewById(R.id.bookCover);
if (bitmap != null)
    imageView.setImageBitmap(bitmap);
else
    imageView.setVisibility(View.GONE);