2014-02-10 1 views
0

이전에 소켓을 통해 비디오 파일 만 전송하지만 다른 쪽에서 파일 크기가 필요할 때 문제가 발생하여 소켓을 통해 Object를 보내려했지만 파일에 문제가 발생했습니다. 크기 제한. 최대 45MB의 크기까지 파일을 전송하지만 그 이상의 크기의 파일을 전송하고 싶습니다. 아래
내가 게시 한 코드 :큰 파일을 안드로이드의 소켓을 통해 파일 크기와 함께 보내십시오.

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

u pls가 n xampl 으로 정교 할 수 있습니다. u 감사합니다. – user3210749

+0

다음과 같은 코드를 시도해보십시오 :'buf = new byte [1000]; \t \t \t 동안 (fis.read (BUF) = -1!) { \t \t \t \t fos.write (BUF); \t \t \t} – ZhangLei