2014-10-10 43 views
-1
double bytes = file.length(); 
            double kilobytes = (bytes/1024); 
            double megaBytes = (kilobytes/1024); 


           if(megaBytes<10) 
           { 
            filesize = Double.toString(kilobytes); 
            System.out.println("kilobytes String : " + filesize); 
            try 
            { 
             arrByteFile = null; 

             int bytesRead; 
             int bufferSize = 1024; 
             FileInputStream is = new FileInputStream(file); 
             ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
             byte[] b = new byte[bufferSize]; 

             while ((bytesRead = is.read(b)) != -1) 
             { 
              bos.write(b, 0, bytesRead); 
             } 

             arrByteFile = bos.toByteArray(); 
             is.close(); 

             System.out.println("File byte array Size:-"+arrByteFile.length); 
            } 
            catch(Exception e) 
            { 
             e.printStackTrace(); 
            } 


            try 
            { 
             ChunkOfByteArray chunkOfByteArray = new ChunkOfByteArray(arrByteFile); 
             arrayList = chunkOfByteArray.getChunksOfByteArray(); 
            } 
            catch (Exception e) 
            { 
             // TODO Auto-generated catch block 
             e.printStackTrace(); 
            } 

如何處理內存不足錯誤?文件大於3Mb的內存不足錯誤雖然使用塊上載

有無論如何上傳Base64格式的大文件.. ??

我正在使用AQuery jar進行API調用並使用塊來上傳文件。但它僅在創建塊時發生錯誤。

感謝您的時間..

回答

0

問題是你是不是當你讀它發送數據。你正在創建一個大緩衝區,它根本沒有任何幫助,因爲如果你需要這個文件,可以一次讀取一個文件,但是你不需要。

只要在讀取數據時發送數據,並且不會爲更大的文件使用更多內存。即而不是寫入bos直接寫入套接字輸出流。

BTW您可以使用Base64編碼,但是這是更大的,並會使用更多的內存(除非你發送的數據,你讀它,因爲我建議)

+0

是的,先生..˚F或大型文件可能是我將不得不這樣做。感謝您的建議 – nadafafif 2014-10-10 10:10:35

+0

@nadafafif這樣,無論文件大小如何,您都將使用1 KB(對於您的字節[])。 – 2014-10-10 10:15:05

-1

轉至AndroidManifest.xml文件,並把這個:

機器人:largeHeap = 「真」

例如:

<application 
     android:allowBackup="true" 
     android:label="@string/name" 
     android:largeHeap="true" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="pt.example.test" 
      android:label="@string/app_name"> 
      <intent-filter android:label="@string/name" > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application>