2017-03-09 4 views
0

내 앱에서 GD API을 사용하여 Google 드라이브에 파일을 업로드하고 있습니다. 파일 크기가 작은 경우에는 잘 작동하지만 파일 크기가 클 경우 (예 : 200MB) java.lang.OutOfMemoryError: 예외가 발생합니다. 나는 그것이 왜 메모리에 전체 데이터를로드 충돌, 누가 아무도 내가이 문제를 해결할 수있는 제안을 알지? 이 줄은 RAM 200MB의 할당 것이다 확실히 예외 OutOfMemoryError가 발생할 수큰 파일을 Google 드라이브에 업로드

OutputStream outputStream = result.getDriveContents().getOutputStream(); 
FileInputStream fis; 

try { 
    fis = new FileInputStream(file.getPath()); 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    byte[] buf = new byte[8192]; 
    int n; 
    while (-1 != (n = fis.read(buf))) 
      baos.write(buf, 0, n); 
    byte[] photoBytes = baos.toByteArray(); 
    outputStream.write(photoBytes); 

    outputStream.close(); 
    outputStream = null; 
    fis.close(); 
    fis = null; 
} catch (FileNotFoundException e) {     
} 
+0

의심 할 여지없이 더 작은 버퍼를 사용하여 청크로 작성해야합니다. –

+0

몇 가지 예를 보여줄 수 있습니까? –

답변

2

: 당신이 당신의 outputStream에 직접 작성하지 않는 이유는

byte[] photoBytes = baos.toByteArray(); 

을 :

내 코드입니다
while (-1 != (n = fis.read(buf))) 
     outputStream.write(buf, 0, n);