2015-07-05 22 views
0

我正在從遠程服務器下載音頻文件,然後打算在應用程序上實際播放它之前將其保存到存儲空間。 我使用sliderbridge類來顯示進度,但是當我嘗試將其保存到存儲我得到下載完成這個錯誤:java.io.FileNotFoundException在Codenameone

   java.io.FileNotFoundException 

相關代碼:

  public void actionPerformed(ActionEvent ae) { 
      final String file=_cat+".mp3"; 
     if(FileSystemStorage.getInstance().exists(file)) 
     {FileSystemStorage.getInstance().delete(file);}; 
     String path="/"+file; 

       ConnectionRequest r = new ConnectionRequest() { 




        protected void postResponse() { 
     try{ 
      String path1=FileSystemStorage.getInstance().getAppHomePath(); 
      String path2=path1+"/"+file; 
      InputStream stream =FileSystemStorage.getInstance().openInputStream(path2); 
      OutputStream out = Storage.getInstance().createOutputStream(file); 
      Util.copy(stream, out); 
      Util.cleanup(stream); 
      Util.cleanup(out); 
     } 
     catch(Exception ex) 
     {ex.printStackTrace();} 
        } 

      protected void handleException(Exception err) { 
        Dialog.show("Network Error!!", "Are you connected to the internet? Check your connection...And Click To Start Again", "Ok", null); 
       overall.removeComponent(Welcome); 
       overall.removeComponent(sb); 
       overall.invalidate(); 
       overall.revalidate(); 
      } 
     }; 


     r.setUrl("http://abcde.com/images/"+file); 
     r.setPost(false); 

     try 
     { 
      sb=new SliderBridge(r); 
      NetworkManager.getInstance().addToQueue(r); 
      overall.addComponent(Welcome); 
      overall.addComponent(sb); 
      overall.repaint(); 
      overall.invalidate(); 
      overall.revalidate(); 


      } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

回答

0

我得到它的工作...我用這個

 ConnectionRequest r; 
     r = new ConnectionRequest() { 
     int chr; 
     int size=1024; 
     byte[] data = new byte[size]; 
     ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
     byte[] final_dat; 
     @Override 
     protected void readResponse(InputStream input) throws IOException { 
     while ((chr = input.read(data,0,size)) != -1){ 
     buffer.write(data,0, chr); 


        } 
      final_dat=buffer.toByteArray(); 



      } 


     protected void postResponse() { 

     try{ 

     isp=new ByteArrayInputStream(final_dat); 
     OutputStream out = Storage.getInstance().createOutputStream(file); 
     } 
     Util.copy(isp, out); 
     Util.cleanup(isp); 
     Util.cleanup(out); 
     } 
    catch(Exception ex) 
    {ex.printStackTrace();} 
}