我试图做这样的事情:
公共无效的onClick(视图v){ WebServiceC web服务= WebServiceC(); epubDownloaded = webService.downloadEpub(publi.getId()); 文件fileEpub =新的文件(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文件
发布评论