2015-01-05 14 views
8

Tôi đang cố gắng chuyển đổi tệp từ sdcard sang Base64 nhưng có vẻ như tệp quá lớn và tôi nhận được lỗi OutOfMemoryError.Chuyển đổi tệp (<100Mo) trong Base64 trên Android

Đây là mã của tôi:

InputStream inputStream = null;//You can get an inputStream using any IO API 
inputStream = new FileInputStream(file.getAbsolutePath()); 
byte[] bytes; 
byte[] buffer = new byte[8192]; 
int bytesRead; 
ByteArrayOutputStream output = new ByteArrayOutputStream(); 
try { 
    while ((bytesRead = inputStream.read(buffer)) != -1) { 
     output.write(buffer, 0, bytesRead); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
bytes = output.toByteArray(); 
attachedFile = Base64.encodeToString(bytes, Base64.DEFAULT); 

Có cách nào để đi xung quanh OutOfMemoryError khi nộp String attachedFile?

+0

Sử dụng mã này nó sẽ rất hữu ích https://stackoverflow.com/a/47572643/3505534 – R2R

Trả lời

8

Base64 mã hóa có 3 byte đầu vào và chuyển đổi chúng thành 4 byte. Vì vậy, nếu bạn có tệp 100 Mb sẽ kết thúc là 133 Mb trong Base64. Khi bạn chuyển đổi nó thành chuỗi Java (UTF-16), kích thước của nó sẽ được tăng gấp đôi. Chưa kể rằng trong quá trình chuyển đổi tại một số thời điểm bạn sẽ giữ nhiều bản sao trong bộ nhớ. Không có vấn đề làm thế nào bạn bật này nó là hầu như không đi làm việc.

Đây là mã được tối ưu hóa cao hơn một chút sử dụng Base64OutputStream và sẽ cần ít bộ nhớ hơn mã của bạn, nhưng tôi sẽ không nín thở. Lời khuyên của tôi là cải thiện mã đó hơn nữa bằng cách bỏ qua chuyển đổi thành chuỗi và sử dụng luồng tệp tạm thời làm đầu ra thay vì ByteArrayOutputStream.

InputStream inputStream = null;//You can get an inputStream using any IO API 
inputStream = new FileInputStream(file.getAbsolutePath()); 
byte[] buffer = new byte[8192]; 
int bytesRead; 
ByteArrayOutputStream output = new ByteArrayOutputStream(); 
Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT); 
try { 
    while ((bytesRead = inputStream.read(buffer)) != -1) { 
     output64.write(buffer, 0, bytesRead); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
output64.close(); 

attachedFile = output.toString(); 
+1

Cảm ơn bạn đã trả lời của bạn! Tôi sẽ xem xét nhiều phần (với trang bị thêm) và khác ... – Labe

+0

hoạt động tốt, cảm ơn –

0
// Converting File to Base64.encode String type using Method 
    public String getStringFile(File f) { 
     InputStream inputStream = null; 
     String encodedFile= "", lastVal; 
     try { 
      inputStream = new FileInputStream(f.getAbsolutePath()); 

     byte[] buffer = new byte[10240];//specify the size to allow 
     int bytesRead; 
     ByteArrayOutputStream output = new ByteArrayOutputStream(); 
     Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT); 

      while ((bytesRead = inputStream.read(buffer)) != -1) { 
       output64.write(buffer, 0, bytesRead); 
      } 


     output64.close(); 


     encodedFile = output.toString(); 

     } 
     catch (FileNotFoundException e1) { 
       e1.printStackTrace(); 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 
     lastVal = encodedFile; 
     return lastVal; 
    } 
Các vấn đề liên quan