2014-02-10 171 views
0

之前我只通過套接字傳輸視頻文件,但是當我需要在另一端文件大小然後出現問題,所以我想通過套接字發送對象,但出現文件問題大小限制。它傳輸文件大小爲45MB,但我想傳輸的文件大小超過。
下面 我已經發布代碼:發送大文件連同文件大小通過Android

public class WiFiDirectBundle implements Serializable { 
private String fileName; 
private String fileType; 
private Long fileSize; 

ArrayList<byte[]> chunks; 
ArrayList<Integer> a; 
static int len=0; 
byte[] buf; 

public WiFiDirectBundle() { 
    // TODO Auto-generated constructor stub 
} 

public void setFile(String path) throws FileNotFoundException, 
IOException { 
    File f = new File(path); 

    fileName = f.getName(); 
    fileType = MimeTypeMap.getFileExtensionFromUrl(f.getAbsolutePath()); 
    fileSize = f.length(); 
    Log.d(WiFiDirectActivity.TAG,"name of file is"+fileName); 
    Log.d(WiFiDirectActivity.TAG,"size of file is"+fileSize); 

    FileInputStream fin = null; 
    try { 
     fin = new FileInputStream(f); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 



    fileContent = new byte[(int) f.length()]; 
    try { 
     fin.read(fileContent); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    fin.close(); 

} 

// restores the file of the bundle, given its directory (change to whatever 
// fits you better) 
public String restoreFile(String baseDir) throws IOException { 
    File f = new File(baseDir + "/" + fileName); 
    File dirs = new File(f.getParent()); 
    if (!dirs.exists()) 
     dirs.mkdirs(); 
    f.createNewFile(); 
    try { 
     FileOutputStream fos = new FileOutputStream(f); 
     if (fileContent != null) { 
      fos.write(fileContent); 
     } 


     fos.close(); 
     return f.getAbsolutePath(); 

    } catch (IOException e) { 
      e.printStackTrace(); 
    } 

    return null; 
} 

}

回答

0

我覺得這fileContent = new byte[(int) f.length()];在你的代碼可能會導致Out of memory。正如您聲明byte[] buf;,爲什麼不使用buf呢?

+0

你可以用n xampl 來詳細說明,那可能是很好的哦 謝謝你 – user3210749

+0

你可以試一下這樣的代碼:'buf = new byte [1000]; \t \t \t而(fis.read(BUF)!= -1){ \t \t \t \t fos.write(BUF); \t \t \t}' – ZhangLei