如何从一个字节数组下载EPUB文件

编程入门 行业动态 更新时间:2024-10-11 03:15:22
本文介绍了如何从一个字节数组下载EPUB文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图做这样的事情:

公共无效的onClick(视图v){            WebServiceC web服务= WebServiceC();            epubDownloaded = webService.downloadEpub(publi.getId());             文件fil​​eEpub =新的文件(Environment.getExternalStorageDirectory(),test.epub);             如果(fileEpub.exists()){                fileEpub.delete();              }             尝试{                    FOS的FileOutputStream =新的FileOutputStream(arquivoEpub.getPath());                    fos.write(epubDownloaded);                    fos.close();                  }赶上(IOException异常五){                        e.printStackTrace();                    }        }    });

不过,这是行不通的。

  

致命异常:在主显示java.lang.NullPointerException  java.io.OutputStream.write(OutputStream.java:82)

------------------------------------编辑---------- --------------------------------谢谢..我解决我的问题这种方式 - >

诠释计数;                    WebServiceC web服务=新WebServiceC();                    epubDownloaded = webService.downloadEpub(publi.getId());                    输入的InputStream =新ByteArrayInputStream进行(epubDownloaded);                    OutputStream的输出= NULL;                    尝试{                        输出=新的FileOutputStream(Environment.getExternalStorageDirectory()+/ test.epub);                    }赶上(FileNotFoundException异常五){                        e.printStackTrace();                    }                    字节的数据[] =新的字节[1024];                    尝试{                        而((计数= input.read(数据))!= - 1){                            output.write(数据0,计);                        }                    }赶上(IOException异常五){                        // TODO自动生成catch块                        e.printStackTrace();                    }

解决方案

在code中的唯一的地方,我可以看到一个 NullPointerException异常一个可以引发在该行的FileOutputStream FOS =新的FileOutputStream(arquivoEpub.getPath());

只有时间的FileOutputStream 的构造函数可以抛出一个 NullPointerException异常是当它传递一个空值,其构造的FileOutputStream(的FileDescriptor fdObj)。我不知道有什么功能 arquivoEpub.getPath()的回报,所以我不能做任何其他的猜测。

另外,如果可能的话,后期到底是哪行相当于82号线,因为这是该异常被抛出就行了。

I'm trying to do something like:

public void onClick(View v) { WebServiceC webService = WebServiceC(); epubDownloaded = webService.downloadEpub(publi.getId()); File fileEpub=new File(Environment.getExternalStorageDirectory(), "test.epub"); if (fileEpub.exists()) { fileEpub.delete(); } try { FileOutputStream fos=new FileOutputStream(arquivoEpub.getPath()); fos.write(epubDownloaded); fos.close(); }catch (IOException e) { e.printStackTrace(); } } });

But it doesn't work.

FATAL EXCEPTION: main java.lang.NullPointerException at java.io.OutputStream.write(OutputStream.java:82)

------------------------------------EDIT------------------------------------------ Thanks .. I solved my problem this way ->

int count; WebServiceC webService = new WebServiceC(); epubDownloaded = webService.downloadEpub(publi.getId()); InputStream input = new ByteArrayInputStream(epubDownloaded); OutputStream output = null; try { output = new FileOutputStream(Environment.getExternalStorageDirectory()+"/test.epub"); } catch (FileNotFoundException e) { e.printStackTrace(); } byte data[] = new byte[1024]; try { while ((count = input.read(data)) != -1) { output.write(data, 0, count); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }

解决方案

The only place in your code that I can see that a NullPointerException that can be thrown is in the line FileOutputStream fos=new FileOutputStream(arquivoEpub.getPath());.

The only time the constructor of FileOutputStream can throw a NullPointerException is when it is passed a null value with its constructor FileOutputStream(FileDescriptor fdObj). I don't know what the function arquivoEpub.getPath() returns, so I cannot make any other guesses.

Also, if possible, post exactly which line corresponded to line 82, as that is the line that the exception is thrown on.

更多推荐

如何从一个字节数组下载EPUB文件

本文发布于:2023-11-07 15:54:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566865.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字节   文件   EPUB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!