2012-02-22 77 views
1

我是個試圖上傳從機器人到遠程服務器上的畫面。一切工作都很好,除了大照片。我在Base64.encodeBytes()方法中獲得「內存超出異常」。這是代碼。的Base64 encodeBytes - 存儲器不足異常

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want. 
byte [] byte_arr = stream.toByteArray(); 
String image_str = Base64.encodeBytes(byte_arr); 

你們可以幫我解決這個問題?我曾嘗試通過對位圖進行取樣來解決此問題,並且它可以正常工作,但它會縮小位圖,並且我總是希望以其原始大小上傳圖片。

謝謝。

+0

PNG是一種無損格式,所以它不使用質量設置。應該使用JPEG而不是 – Hrafn 2013-03-19 11:33:24

回答

0

我發現關於上傳圖像的精確原始尺寸的服務器解決方案:

public static void upload(String pathToOurFile, String serverUrl){ 
    HttpURLConnection connection = null; 
    DataOutputStream outputStream = null; 
    DataInputStream inputStream = null; 

    String urlServer = serverUrl; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 

    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1*1024*1024; 

    try 
    { 
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); 

    URL url = new URL(urlServer); 
    connection = (HttpURLConnection) url.openConnection(); 

    // Allow Inputs & Outputs 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    connection.setUseCaches(false); 

    // Enable POST method 
    connection.setRequestMethod("POST"); 

    connection.setRequestProperty("Connection", "Keep-Alive"); 
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

    outputStream = new DataOutputStream(connection.getOutputStream()); 
    outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); 
    outputStream.writeBytes(lineEnd); 

    bytesAvailable = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    buffer = new byte[bufferSize]; 

    // Read file 
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

    while (bytesRead > 0) 
    { 
    outputStream.write(buffer, 0, bufferSize); 
    bytesAvailable = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
    } 

    outputStream.writeBytes(lineEnd); 
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

    // Responses from the server (code and message) 
    int serverResponseCode = connection.getResponseCode(); 
    String serverResponseMessage = connection.getResponseMessage(); 

    fileInputStream.close(); 
    outputStream.flush(); 
    outputStream.close(); 
    } 
    catch (Exception ex) 
    { 
    //Exception handling 
    } 
} 
+0

這個方法可以在webview posturl上使用嗎? – Sunny 2013-10-24 08:40:26

1

我收到了同樣的問題,對於大型圖像...我剛剛把它轉換爲Base64 ......你可以使用它作爲以下之前縮放位圖...

bitmap=Bitmap.createScaledBitmap(bitmap, 100, 100, true); 

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); //compress to which format you want. 
byte [] byte_arr = stream.toByteArray(); 
String image_str = Base64.encodeBytes(byte_arr); 
+0

,但它的縮放bimap儀式?我想上傳它的原始圖像。 – 2012-02-22 09:30:40

+0

是的,這降低了圖像 – 2017-02-13 07:37:13

0

你可以嘗試改變壓縮質量參數像我剛剛給10 &它也適用於大圖像工作。

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 10, stream); 
byte [] byte_arr = stream.toByteArray(); 
String image_str = Base64.encodeBytes(byte_arr); 
+0

質量i'l試試看。 Thnx迴應。 – 2012-02-22 09:30:56

+0

對不起,它沒有工作。異常仍然被拋出。 – 2012-02-22 09:33:56

+0

這是什麼導入你的項目導入android.util.Base64;如果是,則嘗試使用以下URL並在您的項目中導入Base64.java,然後檢查。 http://androidcodemonkey.blogspot.in/2010/03/how-to-base64-encode-decode-android.html – Sumant 2012-02-22 09:56:00

0

基於這些語句:

1 - 我媽想上傳到Android的遠程服務器上的圖片。

2-但其縮放儀式bimap的??我想上傳它的 原始圖像。

只要解決方案可以幫助您從android應用程序上傳文件到服務器並保留原始圖像,我會假設任何答案都會執行。

解決方案 - 取下壓縮了下面的教程,它應該給你對你的方式。

http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

避免Base64.encodeBytes()大型圖像文件。